Say I have a string like this
"some3random5string8"
I want to insert spaces after each integer so it looks like this
"some3 random5 string8"
I specifically want to do this using gsub but I can’t figure out how to access the characters that match my regexp.
For example:
temp = "some3random5string8"
temp.gsub(/\d/, ' ') # instead of replacing with a ' ' I want to replace with
# matching number and space
I was hoping there was a way to reference the regexp match. Something like $1 so I could do something like temp.gsub(/\d/, "#{$1 }") (note, this does not work)
Is this possible?
From the
gsubdocs:This means the following 3 versions will work
Edit: also if you don’t want to add an extra space at the end, use a negative lookahead for the end of line, e.g.:
A positive lookahead checking for a “word character” would also work of course:
Last but not least, the simplest version without a space at the end: