My understanding is that Java's implementation of regular expressions is based on Perl's. However, in the following example, if I execute the same regex with the same string, Java and Perl return different results.
Here's the Java example:
public class RegexTest {
public static void main( String args[] ) {
String sentence = "This is a test of regular expressions.";
System.out.println( sentence.matches( "\\w" ) ? "Matches" : "Doesn't match" );
}
}
This returns: Doesn't match
Here's the Perl example:
my $sentence = 'This is a test of regular expressions.';
print ( $sentence =~ /\w/ ? "Matches" : "Doesn't match" ) . "\n";
This returns: Matches
To me, the Perl result makes sense. It looks for a match for a single word character. I don't understand why Java doesn't consider it a match. What's the reason for the difference?