Could somebody please show me a way to find, and display multiple
regex matches in the same line?
test_string = “I shot the Sherrif but I didn’t shot the Deputy.”
The word I would like to find is the “shot” and displayed like
“shotshot” or like each match could go into a new #line like:
Some ways I tried:
regex = /shot/ =~ test_string
puts $& ------------> # would only match the
regex = /(shot)*/ =~ test_string
puts $& -------------> # only applys repetition
if the word appears next to each other with no spaces
# in between like
“shotshot”. “shot shot” or “shot (some text) shot” would fail.
puts regex = test_string.grep/shot/ ------------> # From Ruby 1.9
String are not enumerable therfore it would give
“no method error”.
I could solve the problem without using regexes but I would like to
know how to solve it with regex.
I hope I was clear enough in describing the problem. I am new to
programming, and ruby is my first language.