r - Insert a non-character if a criteria is TRUE -


is possible insert non-character, in case -, if particular criteria met?

for example: if there 5 numeric characters (12345), insert - after 2nd numeric character (12-345).

i trying fix street addresses.

thanks!

s = "abc 12345 def" sub("([0-9]{2})([0-9]{3})", "\\1-\\2", s) # "abc 12-345 def" 

this find first instance of 5 numbers in row , add "-" after second number. see http://stat.ethz.ch/r-manual/r-patched/library/base/html/regex.html r regex syntax.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -