Why isn't the Java matches method working? (Java pattern matching)

Java Matcher problem: You're trying to use the matches method of the Java Matcher class to match a regular expression (regex) you have defined, and it's not working against a given string, and you don't know why.

Solution: The important thing to remember about this Java matches method is that your regular expression must match the entire line. Specifically, a regex pattern like the following one will not work with the matches method when you work on a larger line of input text:

" year "

However, by modifying this pattern to look like this:

".*year.*"

it will work with the matches method.

Two possible solutions

There are actually two possible solutions to this problem:

  1. Modify your regex pattern to match the entire String, and keep using the matches method.
  2. Modify your Java program to use the find method of the Matcher class instead of using the matches method.

Here's some source code for a complete Java class that demonstrates the matches method, showing both (a) the wrong way and (b) the correct way to define a regex pattern for use with the matches method:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Demonstrates a common error when attempting to use the
 * "matches" method.
 * 
 * The matches() method attempts to match the entire input 
 * sequence against the pattern, so if you're going to try
 * to use it, your pattern must match the entire string.
 * 
 */
public class PatternMatcherMatchesError
{
  public static void main(String[] args)
  {
    // the string we want to search
    String stringToSearch = "Four score and seven years ago our fathers ...";
    
    // search for this simple pattern
    String patternToSeachFor = " year ";          // doesn't match
    //String patternToSeachFor = ".*year.*";      // does match

    // set everything up
    Pattern p = Pattern.compile(patternToSeachFor, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(stringToSearch);
    
    // now see if we find a match
    if (m.matches())
      System.out.println("Found a match");
    else
      System.out.println("Did not find a match");
  }
}

The pattern currently not commented-out makes this program print this output:

Did not find a match

But the other pattern will find a match.

matches() method - Discussion

The Matcher class Javadoc states, "The matches() method attempts to match the entire input sequence against the pattern." Therefore, your pattern must match the entire input sequence.

The find() method solution

As mentioned earlier, another approach is to use the find method of the Matcher class. The find method does not require your pattern to match the entire String. To implement this solution, just replace this line of code above:

if (m.matches())

with this line of code:

if (m.find())