Uhhhh, there seems to be a problem. "abcd12".matches("\\D"); should return true, but doesn't. Last time I checked abc AND d were all non-numeric characters. "abcd".matches("\\D"); works just fine, but as soon as you put a number in the mix, it fails. Any smart readers out there that can enlighten me? For now, I am going to use Perl5Util since it acts as expected.
Posted by carl at June 8, 2003 05:10 AM
This is a common gotcha. It caught me up when I first used Java regexps.
In Perl, a regexp comparison is considered a match if it matches any part of the string, to mirror the behaviour of the 'grep' program. So "abc" =~ /a/; is true in Perl.
In Java, a regexp is only considered to match if it matches the _entire_ string. So "abc".matches("a"); is false, but "abc".matches("a.."); or "abc".matches(".*a.*"); will be true.
Posted by: Charles Miller at June 17, 2003 02:55 AM
Comments