可以通过 Pattern 类的 matcher(CharSequence input) 方法,创建匹配给定输入与此模式的匹配器。匹配器创建后,可以使用它执行三种不同的匹配操作:
matches:方法尝试将整个输入序列与该模式匹配
lookingAt:尝试将输入序列从头开始与该模式匹配
find:方法扫描输入序列以查找与该模式匹配的下一个子序列
尝试将整个输入字符序列与模式匹配。如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
注意:当且仅当整个输入字符序列匹配此匹配器的模式时才返回 true。
实例:使用模式完全匹配 “Hello world.” 文本,代码如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo18 {
    public static void main(String[] args) {
        String str = "Hello world.";
        Pattern pattern = Pattern.compile("([a-zA-Z0-9]+)\\s+([a-z]+)\\.");
        Matcher matcher = pattern.matcher(str);
        if(matcher.matches()) {
            System.out.println("匹配成功");
            System.out.println("分组数:" + matcher.groupCount());
            // 下标为0的分组为主分组
            System.out.println("主分组:" + matcher.group(0));
            for(int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("分组" + i + ": " + matcher.group(i));
            }
        } else {
            System.out.println("匹配失败");
        }
        // 验证是否匹配整个输入字符序列
        System.out.println( pattern.matcher("Hello world. regex test")
                .matches() ? "匹配成功" : "匹配失败");
    }
}运行结果如下:
匹配成功 分组数:2 主分组:Hello world. 分组1: Hello 分组2: world 匹配失败
该方法尝试将从区域开头开始的输入序列与该模式匹配。与 matches 方法类似,此方法始终从区域的开头开始;与之不同的是,它不需要匹配整个区域。如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
注意:当且仅当输入序列的前缀匹配此匹配器的模式时才返回 true。
实例:匹配所有已 “Hello” 开头行。代码如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo19 {
    public static void main(String[] args) {
        String str = "Hello world. regex test";
        Pattern pattern = Pattern.compile("([a-zA-Z0-9]+)\\s+([a-z]+)\\.");
        Matcher matcher = pattern.matcher(str);
        if(matcher.lookingAt()) {
            System.out.println(matcher.group()
                    + " 位置:[" + matcher.start() + ", " + matcher.end() + "]");
        }
        // 验证 lookingAt 是不是前缀匹配
        matcher = pattern.matcher("hi! Hello world.");
        if(matcher.lookingAt()) {
            System.out.println(matcher.group()
                    + " 位置:[" + matcher.start() + ", " + matcher.end() + "]");
        }
    }
}运行结果如下:
Hello world. 位置:[0, 12]
从上面结果可知,lookingAt() 方法匹配字符串的前缀。第二次调用 matcher 的参数为 “hi! Hello world.”,开头并不匹配 “([a-zA-Z0-9]+)\\s+([a-z]+)\\.” 模式。
该方法尝试查找与该模式匹配的输入序列的下一个子序列。此方法从匹配器区域的开头开始,如果该方法的前一次调用成功了并且从那时开始匹配器没有被重置,则从以前匹配操作没有匹配的第一个字符开始。如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
注意:当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true。
实例:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo20 {
    public static void main(String[] args) {
        String str = "Be honest rather clever\r\n" +
                "Being on sea, sail; being on land, settle\r\n" +
                "Be just to all, but trust not all\r\n" +
                "Believe not all that you see nor half what you hear\r\n" +
                "Be slow to promise and quick to perform\r\n" +
                "Between two stools one falls to the ground\r\n" +
                "Better an open enemy than a false friend";
        Pattern pattern = Pattern.compile("\\s+t[a-z]*o\\s+", Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(str);
        while(matcher.find()) {
            System.out.println(matcher.group()
                    + " 位置:[" + matcher.start() + ", " + matcher.end() + "]");
        }
    }
}运行结果如下:
to 位置:[75, 79] to 位置:[163, 167] to 位置:[184, 188] two 位置:[204, 209] to 位置:[225, 229]