Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this regex work as expected in Java?

Tags:

java

regex

trivial regex question (the answer is most probably Java-specific):

"#This is a comment in a file".matches("^#")

This returns false. As far as I can see, ^ means what it always means and # has no special meaning, so I'd translate ^# as "A '#' at the beginning of the string". Which should match. And so it does, in Perl:

perl -e "print '#This is a comment'=~/^#/;"

prints "1". So I'm pretty sure the answer is something Java specific. Would somebody please enlighten me?

Thank you.

like image 317
0xCAFEBABE Avatar asked Jan 25 '11 09:01

0xCAFEBABE


2 Answers

Matcher.matches() checks to see if the entire input string is matched by the regex.

Since your regex only matches the very first character, it returns false.

You'll want to use Matcher.find() instead.

Granted, it can be a bit tricky to find the concrete specification, but it's there:

  • String.matches() is defined as doing the same thing as Pattern.matches(regex, str).
  • Pattern.matches() in turn is defined as Pattern.compile(regex).matcher(input).matches().
    • Pattern.compile() returns a Pattern.
    • Pattern.matcher() returns a Matcher
  • Matcher.matches() is documented like this (emphasis mine):

    Attempts to match the entire region against the pattern.

like image 132
Joachim Sauer Avatar answered Oct 03 '22 06:10

Joachim Sauer


The matches method matches your regex against the entire string.

So try adding a .* to match rest of the string.

"#This is a comment in a file".matches("^#.*")

which returns true. One can even drop all anchors(both start and end) from the regex and the match method will add it for us. So in the above case we could have also used "#.*" as the regex.

like image 23
codaddict Avatar answered Oct 03 '22 06:10

codaddict