rails, ruby - Given a Regex - Determine the Match Count
If you're trying to count the number of matches, then you're using the wrong method. split
is designed to take a string and chop it into bits, but as you've observed, if there aren't any matches, then it returns the whole thing. I think you want to use String.scan
instead:
message.scan(/\n.* at.* XXXXXXXX wrote:.*/m).size
Well split will return an array. So you could just check for length > 1
m = message.split(/\n.* at.* XXXXXXXX wrote:.*/m)
if m.length > 1
return m.first
else
return nil
end