0

I have a string similar to 98abcd123fgh324ijklm564asd. I want to insert a couple of spaces before and after each number in the string. Below is the sample output:

str = "98abcd123fgh324ijklm564asd"

required_function(str)
# " 98 abcd 123 fgh 324 ijklm 564 asd"
Oshan
  • 176
  • 15
  • That sounds like a task for us, what have you tried and what problem you have with that code/regex? – Wiktor Stribiżew Apr 01 '16 at 10:42
  • @WiktorStribiżew I dont understand regexes very well. Instead, I created two columns. Column1 with all the alphabets replacing the digits with space and Column2 with all the numbers, replacing the alphabets with space. That suffices the requirement. I was thinking if this could be done with regex in more efficient way. – Oshan Apr 01 '16 at 10:47
  • 2
    You see, it is *basic* regex: `gsub("(\\d+)", " \\1 ", str)`. Really, I think you need to read [Learning Regular Expressions](http://stackoverflow.com/a/2759417/3832970). – Wiktor Stribiżew Apr 01 '16 at 10:48

1 Answers1

2
str = "98abcd123fgh324ijklm564asd"
gsub("(\\d+)", " \\1 ", str)
rock321987
  • 10,942
  • 1
  • 30
  • 43