Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Wednesday January 28, 2009
11:40 AM

Java gravel of the day: String.match() is anchored

[ #38358 ]

In Java, String.match() conveniently takes a regular expression, not just an ordinary String. However, I just found the following:

"2".matches("2");  // returns true; good
"2007".matches("2");  // returns false -- huh???

Turns out it looks like matches() is anchored; it's implicitly acting as if the regular expression is wrapped in ^ and $. So the regular expression I supply to matches() has to consume the entire string, from beginning to end, or it doesn't match. Sigh.

"2007".matches("2.*");  // returns true -- sigh

This of course is not specified in the documentation anywhere that I can see. At least, not in the Javadocs for the String.matches() method.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • although you have to really search for it.

    String("foo").matches("bar") says that it's equivalent to Pattern.matches("bar","foo"), which is equivalent to Pattern.compile("bar").matcher("foo").matches(); the Pattern.matcher method returns a Matcher object, whose documentation says «The matches method attempts to match the entire input sequence against the pattern» and «The find method scans the input sequence looking for the next subsequence that matches the patternÂ

  • As dakkar said, it IS documemented, but just barely. This same problem has bitten me about once every 2 months for the last two years (since switching to Java 1.5). You'd think I'd learn...

    If only they made matchesAll() and matchesAny() instead of matches().

    • This same problem has bitten me about once every 2 months for the last two years (since switching to Java 1.5).

      I remember this biting me a few years ago when I was doing text parsing and tagging, before Java 1.5. The project I was working on constantly reminded me of this difference from Perl because I actually had to translate the "prototype" code from C-styled Perl to Java for performance reasons (it was never benchmarked, actually). Sigh.