Nick Vanderbilt
Nick Vanderbilt

Reputation: 38540

ruby regular expression not working with match

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?

Upvotes: 0

Views: 1437

Answers (1)

mipadi
mipadi

Reputation: 411330

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.

Upvotes: 4

Related Questions