0

I am trying to write a regex matcher, where by the string should start with 'Feb' , have a space, and then followed by 2 digits.

    String x = "Feb 04 |";

    String regex = "^Feb d{2}";
    Pattern p = Pattern.compile(regex);


    Pattern pattern =   Pattern.compile(regex);
    Matcher matcher =   pattern.matcher(x);
    while (matcher.find())
    {
        System.out.print("FOUND");
    }

'String regex = "^Feb";' does well to detect if it starts with Feb, but trying to detect there is a space followed by 2 digits.

1
  • but d will just match the letter d - check the documentation Pattern \d is a digit (and in Java strings you need an additional \ to escape the first one: "^Feb \\d{2}") Commented Apr 22, 2019 at 8:42

1 Answer 1

2

The regex pattern ^Feb\s\d{2} matches Feb, a white space, and two digits.

[edit]

^Feb\s\d{2}.*$ if you want to match the full string

Sign up to request clarification or add additional context in comments.

1 Comment

be aware that \s also matches tab and other whitespace characters ([ \t\n\x0B\f\r])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.