1 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
括號(hào)外表示平衡文本。
解決方案是將平衡文本內(nèi)聯(lián)與要在平衡文本旁邊查找的文本匹配。你這樣做:
全局查找:
(?s)(?:(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2$)|(?!to|[()]).)*?((?:(?!(?=\()(?:(?=.*?\((?!.*?\4)(.*\)(?!.*\5).*))(?=.*?\)(?!.*?\5)(.*)).)+?.*?(?=\4)[^(]*(?=\5$))(?!to|[()]).)*)(to)
要查看您找到的內(nèi)容,請(qǐng)?zhí)鎿Q為
<$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 )
演示
正則表達(dá)式字符串:
"(?s)(?:(?=\\()(?:(?=.*?\\((?!.*?\\1)(.*\\)(?!.*\\2).*))(?=.*?\\)(?!.*?\\2)(.*)).)+?.*?(?=\\1)[^(]*(?=\\2$)|(?!to|[()]).)*?((?:(?!(?=\\()(?:(?=.*?\\((?!.*?\\4)(.*\\)(?!.*\\5).*))(?=.*?\\)(?!.*?\\5)(.*)).)+?.*?(?=\\4)[^(]*(?=\\5$))(?!to|[()]).)*)(to)"
正則表達(dá)式可讀代碼:
(?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)
祝你好運(yùn) !
添加回答
舉報(bào)