I know this might seem as a duplicate question, but believe me I searched and tried but didn't find a matching solution so hopefully you can help me.
I'm trying to analyze some text output which is displayed as "key(value) key(value)" into a hash and I have succeeded in doing this but whenever my value contains parenthesis, the capture isn't complete and it only captures until the inner closing parenthesis.
regex used: (\S+?)\((.+?)\)
Here is an example with the text input ==> Regex101
First capture group is the key, second capture group should be the value. As you can see the SCYEXIT key with value 'mqconnectlog.so(LogExit)' only captures up until the inner closing parenthesis: 'mqconnectlog.so(LogExit'
i also tried some variations that had the same result:
(\S+?)\(([^)]+)\)
(\S+?)\(([^)]+(?=\)))\)
I think the biggest problem here is that I need to make both capture groups lazy because there are multiple 'key(value)' pairs on the same line, otherwise it would capture too much characters and also include characters from the next 'key(value)' pair from the same line.
Is there any way to solve this?
Yes if there are parens in there, they will always be balanced. Thank you, this is a great solution, I only have to keep in mind that I now have 3 capture groups instead of 2 but that's a small detail. Will study a bit more on these recursive regular expressions because this is completely new to me. Thank you for your help!
@StijnDeSchutter If this part
(\S+?
Is not going to match parenthesis, you might exclude those from matching using a negated character class as well, preventing some of the backtracking([^\s()]+)(\(((?:(?>[^()]+)|(?2))*)\))
regex101.com/r/TZehdD/1