0

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.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34

4 Answers4

5

The difference is that matches in Java means matches the whole string, not just a part of it as in Perl. It is as if there is an implicit ^ and $ present.

It is handy to know, in Java, that

  • find looks for the match anywhere in the string (and can be iterated)
  • lookingAt looks for the match at the beginning of the string
  • matches wants to match the whole string

Also see

Community
  • 1
  • 1
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

The javadoc for Matcher#matches() states

Attempts to match the entire region against the pattern.

So it is trying to match the entire abc dog def. It's not trying to check if the Pattern appears in that String. Use Matcher#find() for that.

Attempts to find the next subsequence of the input sequence that matches the pattern.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

The problem is using matches instead of find. Matches expects the entire string to match \bdog\b, which it clearly doesn't.

This is what you need:

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";
      Matcher m = p.matcher(text);
      while(m.find())  {
         System.out.println(m.group());
      }
   }
}

Thank you for posting working code in your post, that I can just cut, paste, compile, and run, without having to mess around with it, or figure out the missing pieces.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

Unlike most other languages that just have to match part of the input to be true, java's matches() must match the whole string, so to convert from perl to java, just add.* to each end of the regex:

String text = "abc dog def";
if (text.matches(".*\\bdog\\b.*")) // true
Bohemian
  • 412,405
  • 93
  • 575
  • 722