I need a Regex to catch anything between parenthesis except when it is between the following pattern, an S
char followed by square brackets:
S[]
Like in this sentence:
I am a (test) S[ but i am (not catched)], catch (me (if you can))
^^^^^^ ^^^^^^^^^^^^^^^^^ # should be matched
^^^^^^^^^^^^^^^^^^^^^^^^^^ # should not be matched
It should also catch the nested parenthesis.
I tried to make it work using various example but the closest I get was this one :
(?![^S\[]*\])\(([^()]*|\(([^()]*|\(([^()]*|\([^()]*\))*\))*\))*\)?
=> but it fails when you remove the S
from the test sentence.
Any idea of how to do it?
Edit: Should match like in this case but whith the S into account : https://regex101.com/r/WzECSS/1
Edit: this one should do the trick : (?<!S\[[^\]]+)\((?:[^()]|\([^)]*\))+\)
thanks @ctwheels
Edit: the previous regex fails when you put the parenthesis directly after the square brackets like in:
"I am a (test) S[( but i am (not catched)], catch (me (if you can))"
Has anyone an idea on how to fix this. Thanks.