1 回答
TA贡献1809条经验 获得超8个赞
括号外表示平衡文本。
解决方案是将平衡文本内联与要在平衡文本旁边查找的文本匹配。你这样做:
全局查找:
(?s)(?:(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2$)|(?!to|[()]).)*?((?:(?!(?=\()(?:(?=.*?\((?!.*?\4)(.*\)(?!.*\5).*))(?=.*?\)(?!.*?\5)(.*)).)+?.*?(?=\4)[^(]*(?=\5$))(?!to|[()]).)*)(to)
要查看您找到的内容,请替换为
<$3$6>
之前的示例文本:
to (F(i(r(s)t))) ((S)(e)((c)(o))(n)d) (((((((Third))))))) hello to
where is a to and this is also to
(F(i(r(s)t))) ((S)(e)((c)(o))(n)d) (((((((Third)))))))
((123),(456),(789))
(a turn; a task (a turn of work); to turn; Tongan cause Tongan turn) (dsaf)
This is a you to as well as this to here ( asdf )
然后更换后:
<to>< hello to><
where is a to>< and this is also to><
This is a you to>< as well as this to> here ( asdf )
演示
正则表达式字符串:
"(?s)(?:(?=\\()(?:(?=.*?\\((?!.*?\\1)(.*\\)(?!.*\\2).*))(?=.*?\\)(?!.*?\\2)(.*)).)+?.*?(?=\\1)[^(]*(?=\\2$)|(?!to|[()]).)*?((?:(?!(?=\\()(?:(?=.*?\\((?!.*?\\4)(.*\\)(?!.*\\5).*))(?=.*?\\)(?!.*?\\5)(.*)).)+?.*?(?=\\4)[^(]*(?=\\5$))(?!to|[()]).)*)(to)"
正则表达式可读代码:
(?s)
(?:
(?= \( )
(?:
(?=
.*? \(
(?! .*? \1 )
( # (1 start)
.* \)
(?! .* \2 )
.*
) # (1 end)
)
(?=
.*? \)
(?! .*? \2 )
( .* ) # (2)
)
.
)+?
.*?
(?= \1 )
[^(]*
(?= \2 $ )
| (?! to | [()] )
.
)*?
( # (3 start)
(?:
(?!
(?= \( )
(?:
(?=
.*? \(
(?! .*? \4 )
( # (4 start)
.* \)
(?! .* \5 )
.*
) # (4 end)
)
(?=
.*? \)
(?! .*? \5 )
( .* ) # (5)
)
.
)+?
.*?
(?= \4 )
[^(]*
(?= \5 $ )
)
(?! to | [()] )
.
)*
) # (3 end)
( to ) # (6)
祝你好运 !
添加回答
举报