用简单的英语表达正则表达式

5 java regex

我正在开发一个新的Java项目,因此我正在阅读已有的代码.在代码的一个非常重要的部分,如果找到以下正则表达式,我无法真正告诉他们正在做什么.任何人都可以用简单的英语解释他们做了什么?

1)

 [^,]*|.+(,).+
Run Code Online (Sandbox Code Playgroud)

2)

(\()?\d+(?(1)\))
Run Code Online (Sandbox Code Playgroud)

pol*_*nts 17

下次需要解释正则表达式时,您可以使用explain.plRick Measham 的以下服务:

Regex: [^,]*|.+(,).+

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  [^,]*                    any character except: ',' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    ,                        ','
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
Run Code Online (Sandbox Code Playgroud)
Regex: (\()?\d+(?(1)\))

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (                        group and capture to \1 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \(                       '('
--------------------------------------------------------------------------------
  )?                       end of \1 (NOTE: because you're using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?(1)                    if back-reference \1 matched, then:
--------------------------------------------------------------------------------
    \)                       ')'
--------------------------------------------------------------------------------
   |                        else:
--------------------------------------------------------------------------------
                             succeed
--------------------------------------------------------------------------------
  )                        end of conditional on \1
Run Code Online (Sandbox Code Playgroud)

链接


条件说明

JAVA不支持条件!第二种模式的无条件正则表达式如下:

\d+|\(\d+\)
Run Code Online (Sandbox Code Playgroud)

即数字的非零重复,有或没有周围的括号.

链接


模式深入

这是第一个模式的测试工具

    import java.util.regex.*;
    //...

    Pattern p = Pattern.compile("[^,]*|.+(,).+");
    String[] tests = {
        "",       // [] is a match with no commas
        "abc",    // [abc] is a match with no commas
        ",abc",   // [,abc] is not a match
        "abc,",   // [abc,] is not a match
        "ab,c",   // [ab,c] is a match with separating comma
        "ab,c,",  // [ab,c,] is a match with separating comma
        ",",      // [,] is not a match
        ",,",     // [,,] is not a match
        ",,,",    // [,,,] is a match with separating comma
    };
    for (String test : tests) {
        Matcher m = p.matcher(test);
        System.out.format("[%s] is %s %n", test,
            !m.matches() ? "not a match"
            : m.group(1) != null
               ? "a match with separating comma"
               : "a match with no commas"
        );
    }
Run Code Online (Sandbox Code Playgroud)

结论

  • 要匹配,字符串必须属于以下两种情况之一:
    • 不包含逗号(可能是空字符串)
    • 包含用于分隔两个非空字符串的逗号
  • 在匹配时,\1可以用来区分这两种情况

这里是第二个模式的类似测试工具,不使用条件重写(Java不支持):

    Pattern p = Pattern.compile("\\d+|(\\()\\d+\\)");
    String[] tests = {
        "",       // [] is not a match
        "0",      // [0] is a match without parenthesis
        "(0)",    // [(0)] is a match with surrounding parenthesis
        "007",    // [007] is a match without parenthesis
        "(007)",  // [(007)] is a match with surrounding parenthesis
        "(007",   // [(007] is not a match
        "007)",   // [007)] is not a match
        "-1",     // [-1] is not a match
    };
    for (String test : tests) {
        Matcher m = p.matcher(test);
        System.out.format("[%s] is %s %n", test,
            !m.matches() ? "not a match"
            : m.group(1) != null
               ? "a match with surrounding parenthesis"
               : "a match without parenthesis"
        );
    }
Run Code Online (Sandbox Code Playgroud)

如前所述,这匹配非零数字的数字,可能被括号括起(并\1区分两者).


adh*_*lon 9

1)

[^,]* means any number of characters that are not a comma
.+(,).+ means 1 or more characters followed by a comma followed by 1 or more characters
|  means either the first one or the second one
Run Code Online (Sandbox Code Playgroud)

2)

(\()? means zero or one '('  note* backslash is to escape '('
\d+ means 1 or more digits
(?(1)\)) means if back-reference \1 matched, then ')' note* no else is given
Run Code Online (Sandbox Code Playgroud)

另请注意,括号用于捕获正则表达式的某些部分,当然,除非它们使用反斜杠进行转义