go to previous page   go to home page   go to next page




The matches() method tries to match the entire string with the Pattern.

The lookingAt() method tries to match only the beginning of the string with the Pattern. It is OK if their is a tail to the string that does not match.

Here is a program that shows this:

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

public class SimpleLooking
  public static void main(String[] args)
    // create a Pattern object for a regular expression
    Pattern pattern = Pattern.compile( "x+y+" );    
    // create a Matcher object for a target string     
    Matcher matcher = pattern.matcher( "xxxxxxyyyy AAAAAAA BBBBB" );    
    // match the regular expression with the target string
    if (matcher.lookingAt())                               
      System.out.println( "Match" );     
      System.out.println( "No Match" );

This simple program prints out "Match" because the regular expression x+y+ matches the beginning of the target string "xxxxxxyyyy".


Do you expect capture groups to work with lookingAt()?