我需要一个正则表达式来捕获括号之间的所有内容,除非它介于以下模式之间:S
字符,后跟方括号:
S[]
就像这句话:
I am a (test) S[ but i am (not catched)],catch (me (if you can))
^^^^^^ ^^^^^^^^^^^^^^^^^ # should be matched
^^^^^^^^^^^^^^^^^^^^^^^^^^ # should not be matched
它还应该捕获嵌套的括号。
我尝试使用各种示例来使其工作,但我得到的最接近的是这个示例:
(?![^S\[]*\])\(([^()]*|\(([^()]*|\(([^()]*|\([^()]*\))*\))*\))*\)?
=>,但是当您从测试句子中删除S
时,它会失败。
有什么想法吗?
编辑:在这种情况下应匹配,但考虑到S:https://regex101.com/r/WzECSS/1
编辑:这应该可以解决问题:(?<!S\[[^\]]+)\((?:[^()]|\([^)]*\))+\)
谢谢@ctwheels