I saw this question and answered for javascript regex and the answer was long and very ugly. Curious if someone has a cleaner way to implement in ruby.
Here is what I am trying to achieve:
Test string: "foo bar baz"
Regex: /.*(foo).*(bar).*/
Expected Return: [[0,2],[4,6]]
So, my goal is to be able to run a method running in the test string and regular expression, which will return the indices in which each capture group will correspond. I have included both the start and end indexes of the capture groups in the expected return. I will work on it and add my own potential solutions here along the way too. And of course, if there is a way other than regex that is cleaner / easier for this, this is a good answer too.
source share