亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

匹配括號外的字符串

匹配括號外的字符串

呼喚遠方 2022-10-12 10:21:56
我有一個帶有單詞的字符串,to如何to僅在括號外匹配該單詞(a turn; a task (a turn of work); to turn; Tongan cause Tongan turn) to到目前為止,我已經嘗試過這個正則表達式,不幸的是它沒有用:?<!\()\bto\b(?![\w\s]*[\)])
查看完整描述

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)

祝你好運 !


查看完整回答
反對 回復 2022-10-12
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號