Is it possible to find the index of a match in regex while still getting the match? For example:
str = "foo [bar] hello [world]"
str.match(/\[(.*?)\]/) { |match,idx|
puts match
puts idx
}
Unfortunately, idx is nil in this example.
My real world problem is a string, where I want to replace certain sub strings, that are wrapped in brackets with parentheses, based on some conditions (e.g. if the string is inside a blacklist), e.g. "foo [bar] hello [world]" should become "foo [bar] hello (world)" when the word world is in a blacklist.