有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java匹配括号外的字符串

我有一个带单词to的字符串。只有当单词to在括号外时,如何匹配它

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

to

到目前为止,我已经尝试过这个正则表达式,但不幸的是它不起作用:

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

共 (1) 个答案

  1. # 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)
    

    祝你好运