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

Leave a Comment