The regex that should check for a string contains the specified word

I wrote a regex to check if the string contains the word "Page" and after it any number This is the code:

public static void main(String[] args) {
    String str1 = "12/15/14  7:01:44    Page   10 ";
    String str2 = "12/15/14  7:01:44    Page    9 ";
    System.out.println(containsPage(str2));
}



private static boolean containsPage(String str) {
    String regExp = "^.*Page[ ]{1,}[0-9].$";
    return Pattern.matches(regExp, str); 
}

      

Result: str1: false, str2: true

Can you help me what is wrong?

+3


source to share


2 answers


Change the regex to the following:

String regExp = "^.*Page[ ]{1,}[0-9]+.$";

      

so that it matches one or more digits (hence [0-9]+

).



You also do not need the boundary matches ( ^

and $

) as Pattern#matches

will fit across the front line; and [ ]{1,}

equivalently [ ]+

:

String regExp = ".*Page +[0-9]+.";

      

+5


source


Change it to:

String regExp = "^.*Page[ ]{1,}[0-9]+.$";  //or \\d+

      

[0-9]

matches 9 in the second example, but .

matches a space.



In the first example, [0-9]

1 .

matches, 0 matches, and the remaining space is not mapped. Please note that ^

they $

are not needed here either.

Your regex can be simplified:

String regExp = ".*Page\\s+\\d+.";

      

+1


source







All Articles