2

I have the following (Java) code:

public class TestBlah {
    private static final String PATTERN = ".*\\$\\{[.a-zA-Z0-9]+\\}.*";

    public static void main(String[] s) throws IOException {
        String st = "foo ${bar}\n";

        System.out.println(st.matches(PATTERN));
        System.out.println(Pattern.compile(PATTERN).matcher(st).find());
        System.exit(0);
    }
}

Running this code, the former System.out.println outputs false, while the latter outputs true

Am I not understanding something here?

3 Answers 3

3

This is because the . will not match the new line character. Thus, your String that contains a new line, will not match a string that ends with .*. So, when you call matches(), it returns false, because the new line doesn't match.

The second one returns true because it finds a match inside the input string. It doesn't necessarily match the whole string.

From the Pattern javadocs:

. Any character (may or may not match line terminators)

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

1 Comment

From the same doc: The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.
1

String.matches(..) behaves like Matcher.matches(..). From the documentation of Matcher

find(): Attempts to find the next subsequence of 
        the input sequence that matches the pattern.

matches(): Attempts to match the entire input sequence 
        against the pattern.

So you could think of matches() as if it surrounded your regexp with ^ and $ to make sure the beginning of the string matches the beginning of your regular expression and the end of the string matches the end of the regular expression.

Comments

0

There is a difference between matching a pattern and finding the pattern in a String

  • String.matches() :

    Tells whether or not this string matches the given regular expression.

    Your whole string must match the pattern.

  • Matcher.matches() :

    Attempts to match the entire input sequence against the pattern.

    Again your whole string must match.

  • Matcher.find() :

    Attempts to find the next subsequence of the input sequence that matches the pattern.

    Here you only need a "partial match".


As @Justin said :
Your matches() can't work as the . won't match new line characters (\n, \r and \r\n).


Resources :

Comments

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.