Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match multiple regex patterns in ruby

Tags:

regex

ruby

I see couple of questions on multiple regex patterns in different contexts but I am unable to get a grip on it.

I have a string str = "Hello, how are you. Hello, I am lloyds" in which I would like to apply multiple patterns to extract all Hellos and all lls in one go to get ["Hello", "Hello", "ll", "ll", "ll"]. How do I do it?

The only way I was able to do is (which is not multiple patterns in one go)

str = "Hello, how are you. Hello, I am lloyds"
a = []
a << str.scan(/Hello/)
a << str.scan(/ll/)
a.flatten
like image 890
Bala Avatar asked Jan 12 '23 09:01

Bala


1 Answers

Because "ll" is inside "Hello", logic to include both in same scan method call requires a slightly clumsy-looking expression that double-captures the "ll". This seems close, but note the sequence interleaves "Hello" and "ll", unlike the expected output. However, as far as I can see, that would be a necessity for any regular expression that makes a single pass through the string:

str = "Hello, how are you. Hello, I am lloyds"
a = str.scan( /(He(ll)o|ll)/ ).flatten.compact
 => ["Hello", "ll", "Hello", "ll", "ll"]

The compact is necessary, because a lone "ll" will not match the inner capture, and the array may contain unwanted nils.

like image 152
Neil Slater Avatar answered Jan 22 '23 04:01

Neil Slater