How do I obtain an array of the indexes (or positions) all occurrences of a regexp in a string?
example_string= "hello how are you?
I would like to obtain the array [1,12] for regexp /e/
Here's one approach to get an array of indexes of matches:
example_string = "hello how are you?"
example_string.enum_for(:scan, /e/).map { Regexp.last_match.begin(0) }
# => [1, 12]
Hope it helps!
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