Finding all occurrences

Hello
I found this really interesting example on reg expressions
eg

def show_regexp(string, pattern)
match = pattern.match(string)
if match
"#{match.pre_match}->#{match[0]}<-#{match.post_match}"
else
"no match"
end
end

We could use this method like this:
show_regexp('very interesting', /t/) # => very in->t<-eresting

How could I modify this so I can see all occurrences - i.e.
show_regexp('very interesting', /t/) # => very in->t<-eres->t<-ing

Regards
PHIL SawyerAsked:
Who is Participating?
 
fridomConnect With a Mentor CEO/ProgrammerCommented:
I'm a little under time pressure. You may like to visit:
http://stackoverflow.com/questions/4692437/regex-with-named-capture-groups-getting-all-matches-in-ruby

Looks as if it does something the way you like it to have.

Regards
0
 
PHIL SawyerAuthor Commented:
fridom

very helpful

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.