Java Code:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
String r = "\\bdog\\b";
Pattern p = Pattern.compile(r);
String text = "abc dog def";
System.out.println(p.matcher(text).matches());
}
}
Equivalent Perl Code:
$x = "abc dog def";
if ($x =~ /\bdog\b/) {
print "matches";
}
The Perl code behaves as expected and prints that the string "abc dog def" matches the given regular expression. The Java code on the other hand, says the string does not match the regex. Am I am making some stupid mistake? Interestingly, if I enter the regex on cmd line (not in a string literal) as exemplified by the Oracle Docs, then it works all right.