1

Is it possible to insert a non-character, in this case -, if a particular criteria is met?

For example: If there are five numeric characters (12345), then insert a - after the 2nd numeric character (12-345).

I am trying to fix street addresses.

Thanks!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
yokota
  • 1,007
  • 12
  • 23

1 Answers1

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

This will find first instance of 5 numbers in a row and add a "-" after the second number. See http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html for R regex syntax.

eddi
  • 49,088
  • 6
  • 104
  • 155
  • It's not really clear to me how much of the above to explain, but I'd be happy to answer any specific questions about the expression. For R regex syntax, see http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html – eddi Apr 19 '13 at 22:17
  • Neatly it works for numeric values and for vectors too. It also inserts a "-" if there are more than five numeric characters – Henry Apr 19 '13 at 23:21
  • Thank you everyone for your replies. The solution works great! – yokota Apr 22 '13 at 19:04