gpt4 book ai didi

java - 简单英语的正则表达式

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:49 27 4
gpt4 key购买 nike

我正在处理一个新的 Java 项目,因此我正在阅读已经存在的代码。在代码的一个非常重要的部分,如果发现以下正则表达式,我无法真正知道他们在做什么。任何人都可以用简单的英语解释他们做什么??

1)

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

2)

(\()?\d+(?(1)\))

最佳答案

下次需要解释正则表达式时,可以使用以下 explain.pl来自 Rick 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))

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

链接


注意条件

JAVA 不支持条件语句!第二种模式的无条件正则表达式类似于:

\d+|\(\d+\)

即数字的非零重复,带或不带括号。

链接


深度模式

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

    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"
);
}

结论

  • 要匹配,字符串必须属于以下两种情况之一:
    • 不包含逗号(可能是空字符串)
    • 包含分隔两个非空字符串的逗号
  • 在匹配上,\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"
);
}

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

关于java - 简单英语的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2807448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com