一、Matcher类
package com.jun.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* matcher类
*/
public class MatcherMethod {
public static void main(String[] args) {
String content ="hello tom hello jack yaya yaya";
String regStr ="hello";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println("---------");
System.out.println(matcher.start());
System.out.println(matcher.end());
System.out.println("找到"+content.substring(matcher.start(),matcher.end()));
}
//整体匹配
System.out.println("整体匹配="+matcher.matches());
//如果content有yaya就替换成 老黄牛
regStr ="yaya";
pattern = Pattern.compile(regStr);
matcher = pattern.matcher(content);
//返回的字符串才是替换后的字符串 原来的Content没有变化
String newContent = matcher.replaceAll("老黄牛");
System.out.println("newContent:"+newContent);
System.out.println("Content:"+content);
}
}