Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby regex matching overlapping terms

Tags:

regex

ruby

I'm using:

r = /(hell|hello)/

"hello".scan(r)  #=>   ["hell"]

but I would like to get [ "hell", "hello" ].

http://rubular.com/r/IxdPKYSUAu

like image 531
olivM Avatar asked Sep 24 '26 04:09

olivM


1 Answers

You can use a fancier capture:

'hello'.match(/((hell)o)/).captures
=> ["hello", "hell"]
like image 63
the Tin Man Avatar answered Sep 26 '26 19:09

the Tin Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!