Java 对条件前瞻的支持

发布于 2024-12-28 03:17:49 字数 1099 浏览 4 评论 0原文

下面我们假设邮政编码,我试图从结果中排除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素染倾城色 2025-01-04 03:17:49

要捕获除 33333 之外的所有数字,请使用以下代码:

String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?=(-\\d{4}|\\s|$))(-\\d{4})?";
Matcher m = Pattern.compile(regex).matcher(zip);
while(m.find())
    System.out.printf("Macthed: [%s]%n", m.group(1));

输出:

Macthed: [11111]
Macthed: [22222]
Macthed: [44444-4444]

说明: 该正则表达式使用向前查找,它本身就像一个条件,这意味着匹配必须遵循的 5 位数字由 - 和 4 位数字或空格或字符串结尾,然后可以选择匹配文本 - 和 4 位数字。

您的原始正则表达式抛出异常的原因是您的正则表达式的 ?:(?=-) 部分存在语法错误。

To capture all numbers except 33333 use this code:

String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?=(-\\d{4}|\\s|$))(-\\d{4})?";
Matcher m = Pattern.compile(regex).matcher(zip);
while(m.find())
    System.out.printf("Macthed: [%s]%n", m.group(1));

OUTPUT:

Macthed: [11111]
Macthed: [22222]
Macthed: [44444-4444]

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.

笑着哭最痛 2025-01-04 03:17:49

您在 (? 之后缺少冒号,即使用此正则表达式(非 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.

遮了一弯 2025-01-04 03:17:49

你的问题对我来说有点不清楚。我想您正在寻找:

String st = "11111 22222 33333- 44444-4444";
String pattern = "\\d+(- )";
String res  = st.replaceAll(pattern,"");
System.out.println(res);

Output = 11111 22222 44444-4444

Your question is a little unclear to me. I suppose you are looking for:

String st = "11111 22222 33333- 44444-4444";
String pattern = "\\d+(- )";
String res  = st.replaceAll(pattern,"");
System.out.println(res);

Output = 11111 22222 44444-4444

榕城若虚 2025-01-04 03:17:49
(\d{5}(?!-\s)(?:-\d{4})?)

因此:

String regex = "(\\d{5}(?!-\\s)(?:-\\d{4})?)";`
(\d{5}(?!-\s)(?:-\d{4})?)

hence:

String regex = "(\\d{5}(?!-\\s)(?:-\\d{4})?)";`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文