匹配括号外的字符串

时间:2019-04-18 20:30:51

标签: java regex

我有一个带有单词to的字符串,仅当单词to在方括号之外时如何匹配

(a turn; a task (a turn of work); to turn; Tongan cause Tongan turn)

to

到目前为止,我已经尝试过此Regex,但不幸的是它没有用:

?<!\()\bto\b(?![\w\s]*[\)])

1 个答案:

答案 0 :(得分:1)

方括号外的文本表示平衡。
解决方案是使内联平衡文本与所需文本匹配 查找外侧平衡文本。 您可以这样操作:

全局查找:

(?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 )

Demo

正则表达式字符串:

"(?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)

祝你好运!