Java 对条件前瞻的支持
下面我们假设邮政编码,我试图从结果中排除 33333-
。
我做:
String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?(?=-)-\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(zip);
while (matcher.find()) {
System.out.println(" Found: " + matcher.group());
}
期望得到:
Found: 11111
Found: 22222
Found: 44444-4444
我正在尝试强制执行以下格式:
5 位数字(可选)后跟 - 和 4 位数字。不需要仅带有 -(连字符)的 5 位数字
我收到异常:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown inline modifier near index 7
\d{5}(?(?=-)(-\d{4}))
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
我是否没有正确使用条件前瞻?
In the following let's say zip codes I am trying to exclude the 33333-
from the result.
I do:
String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?(?=-)-\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(zip);
while (matcher.find()) {
System.out.println(" Found: " + matcher.group());
}
Expect to get:
Found: 11111
Found: 22222
Found: 44444-4444
I am trying to enforce format of:
5 digits optionally followed by a - and 4 digits. 5 digits with just a - (hyphen) is not wanted
I get exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown inline modifier near index 7
\d{5}(?(?=-)(-\d{4}))
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
Am I not using the conditional lookahead correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要捕获除 33333 之外的所有数字,请使用以下代码:
输出:
说明: 该正则表达式使用向前查找,它本身就像一个条件,这意味着匹配必须遵循的 5 位数字由 - 和 4 位数字或空格或字符串结尾,然后可以选择匹配文本 - 和 4 位数字。
您的原始正则表达式抛出异常的原因是您的正则表达式的
?:(?=-)
部分存在语法错误。To capture all numbers except 33333 use this code:
OUTPUT:
Explanation: This RegEx is using lookahead that itself is like a condition, which means match 5 digit number which must be followed by - and 4 digits OR a space OR end of string and then it is optionally matching a text - and 4 digits.
The reason why your original RegEx is throwing exception because there is a syntax error in
?:(?=-)
part of your RegEx.您在
(?
之后缺少冒号,即使用此正则表达式(非 Java 字符串):\d{5}(?:(?=-)-\d{4 })
但是,这可能仍然无法产生您想要的结果。请发布一些示例输入和所需的输出。
You'r missing a colon after
(?
, i.e. use this regex (non-Java-String):\d{5}(?:(?=-)-\d{4})
.However, this might still not produce the result you want. Please post some example input and required output.
你的问题对我来说有点不清楚。我想您正在寻找:
Output =
11111 22222 44444-4444
Your question is a little unclear to me. I suppose you are looking for:
Output =
11111 22222 44444-4444
因此:
hence: