簡體   English   中英

正則表達式:匹配僅包含非重復單詞的字符串

[英]Regular Expression :match string containing only non repeating words

我有這種情況(Java代碼):1)字符串如:“狂野冒險”應匹配。 2)帶有相鄰重復單詞的字符串:“狂野野外冒險”不應該匹配。

使用此正則表達式:。* \\ b(\\ w +)\\ b \\ s * \\ 1 \\ b。*我可以匹配包含相鄰重復單詞的字符串。

如何扭轉這種情況,即如何匹配不包含相鄰重復單詞的字符串

使用負前瞻斷言, (?!pattern) ?! (?!pattern)

    String[] tests = {
        "A wild adventure",      // true
        "A wild wild adventure"  // false
    };
    for (String test : tests) {
        System.out.println(test.matches("(?!.*\\b(\\w+)\\s\\1\\b).*"));
    }

解釋由Rick Measham的explain.pl

REGEX: (?!.*\b(\w+)\s\1\b).*
NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    (                        group and capture to \1:
--------------------------------------------------------------------------------
      \w+                      word characters (a-z, A-Z, 0-9, _) (1
                               or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
    )                        end of \1
--------------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
    \1                       what was matched by capture \1
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

也可以看看

相關問題


注意

只有當你想要積極匹配的其他模式時,否定斷言才有意義(參見上面的例子)。 否則,你可以使用布爾補碼運算符! 用你之前使用的任何模式否定matches

String[] tests = {
    "A wild adventure",      // true
    "A wild wild adventure"  // false
};
for (String test : tests) {
    System.out.println(!test.matches(".*\\b(\\w+)\\s\\1\\b.*"));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM