Pattern.COMMENTS 总是导致 Matcher.find 失败

发布于 2025-01-02 10:41:10 字数 1801 浏览 0 评论 0原文

以下代码匹配两个表达式并打印成功。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

但是,我希望空格不重要,因此以下内容也应该打印成功。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id:[0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

Pattern.COMMENTS 标志应该允许空白,但它会导致打印失败。如果字符串完全相同(包括空格),它甚至会导致打印失败,就像第一个示例一样。例如,

导入java.util.regex.Matcher; 导入java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

打印失败。

为什么会发生这种情况以及如何使模式忽略空白?

The following code matches the two expressions and prints success.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

However, I want white space to not matter, so the following should also print success.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id:[0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

The Pattern.COMMENTS flag is supposed to permit white space, but it causes Failure to be printed. It even causes Failure to be printed if the strings are exactly equivalent including white space, like in the first example. For example,

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {

        String regex = "\\{user_id : [0-9]+\\}";
        String string = "{user_id : 0}";

        Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(string);

        if (matcher.find())
            System.out.println("Success.");
        else
            System.out.println("Failure.");
    }
}

Prints Failure.

Why is this happening and how do I make the Pattern ignore white space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

星星的轨迹 2025-01-09 10:41:10

你这边有误会。 Pattern.COMMENTS 允许您在正则表达式中添加额外的空格,以提高正则表达式的可读性,但该空格不会在字符串中匹配。

这不允许字符串中存在空格,然后自动匹配空格,而无需在正则表达式中定义。

示例

使用Pattern.COMMENTS,您可以像这样在正则表达式中添加空格

String regex = "\\{ user_id: [0-9]+ \\}";

以提高可读性,但它不会匹配字符串

String string = "{user_id : 0}";

,因为您尚未在正则表达式中定义空格字符串,所以如果你想使用 Pattern.COMMENTS 那么你需要特殊对待你想要匹配的空白,要么你转义它

String regex = "\\{ user_id\\ :\\  [0-9]+ \\}";

要么你使用空白类

String regex = "\\{ user_id \\s:\\s [0-9]+ \\}";

There is a misunderstanding on your side. Pattern.COMMENTS allow you to put additional whitespace into your regex, to improve the readability of the regex, but this whitespace will NOT be matched in the string.

This does not allow whitespace in your string, that is then matched automatically, without being defined in the regex.

Example

With Pattern.COMMENTS you can put whitespace in your regex like this

String regex = "\\{ user_id: [0-9]+ \\}";

to improve readablitiy, but the it will not match the string

String string = "{user_id : 0}";

because you haven't defined the whitespaces in the string, so if you want to use Pattern.COMMENTS then you need to treat whitespace you want to match specially, either you escape it

String regex = "\\{ user_id\\ :\\  [0-9]+ \\}";

or you use the whitespace class

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