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" ) . "
";
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…