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 Hello
s and all ll
s 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
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 nil
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With