简体   繁体   中英

Java Regex: Word Boundary Matcher in a String Literal

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.

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM