Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern.COMMENTS always causing Matcher.find to fail

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?

like image 701
gsingh2011 Avatar asked Jul 12 '26 05:07

gsingh2011


1 Answers

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]+ \\}";
like image 70
stema Avatar answered Jul 13 '26 21:07

stema



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!