Reputation: 111080
Given something like:
message.split(/\n.* at.* XXXXXXXX wrote:.*/m).first
This works if there is a match, but when there isn't, it just returns all of message
.
Upvotes: 33
Views: 18064
Reputation: 4747
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
Upvotes: 62
Reputation: 20232
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
Upvotes: 1