tags:

views:

48

answers:

1
text = "I fixed bug #1234 and #7895 "
regex = /#(\d*)/
m = regex.match(text)
puts m.inspect #<MatchData "#1234" "1234">

In the above case why I am not seeing 7895? What's the correct solution?

+4  A: 

Regexps only match the first occurrence (or not at all, of course). #(\d*) matches #1234 first, so that piece of text is returned.

If you want multiple matches, i.e., you want to search a string, use String#scan or something similar.

mipadi
Also, you should be using #(\d+) not \d* - The star is for zero or more occurrences and I don't think you want the string 'I fixed bug #invalid' to match.
Andrew Walker
good point. Thanks Andrew.
Nadal