1. Pattern 类
public final class Pattern implements java.io.Serializable {
public static Pattern compile(String regex) {
return new Pattern(regex, 0);
}
public Matcher matcher(CharSequence input) {
if (!compiled) {
synchronized(this) {
if (!compiled)
compile();
}
}
Matcher m = new Matcher(this, input);
return m;
}
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}
}
1.1 模版
public class RegexDemo {
private static final String ENTIRE_REGEX = "(.|\\s)*";
private static final String SUB_SEQUENCE_REGEX = "code\\d+"; // 子串
private static final String GROUP_REGEX = "(name\\d+).*(code\\d+)";
private static final Pattern entireCompile = Pattern.compile(ENTIRE_REGEX);
private static final Pattern subSequenceCompile = Pattern.compile(SUB_SEQUENCE_REGEX);
private static final Pattern groupCompile = Pattern.compile(GROUP_REGEX);
public static void print(Object o) {
System.out.println(o);
}
}
1.2 matches()
matches 是匹配整个字符串
public class RegexDemo {
private static final String REGEX = "[a-z]{3}";
// 先编译这个模式
private static final Pattern pattern = Pattern.compile(REGEX);
@Test
public void matches() {
String input = "xyz";
Matcher matcher = pattern.matcher(input);
boolean matches = matcher.matches();
print(matches);
}
public static void print(Object o) {
System.out.println(o);
}
}
链式写法
@Test
public void matchesLink() {
String input = "xyz";
boolean matches = Pattern.compile(REGEX)
.matcher(input)
.matches();
print(matches);
}
1.3 matches(String regex, CharSequence input)
private static final String ENTIRE_REGEX = ".*match.*";
private static final Pattern entireCompile = Pattern.compile(ENTIRE_REGEX);
@Test
public void match() {
String data = "Attempts to match the entire region against the pattern.";
boolean matches = Pattern.matches(ENTIRE_REGEX, data);
// 等价
boolean matches2 = entireCompile.matcher(data).matches();
}
2. Matcher 类
public final class Matcher implements MatchResult {
public String group() {
return group(0);
}
public boolean find() {
int nextSearchIndex = last;
// ...
return search(nextSearchIndex);
}
public boolean matches() {
return match(from, ENDANCHOR);
}
}
Matcher 可以理解为“某次具体匹配的结果对象”:把编译好的Pattern 对象“应用”到某个String对象上,就获得了作为“本次匹配结果”的Matcher对象
2.1 find()
每调用一次 find() 方法,如果返回 true,就表示“找到一个匹配”
@Test
public void find() {
String str = "123-4567-00";
Matcher matcher = digitPattern.matcher(str);
// 找子串,找到一个子串,然后把该子串去掉
print(matcher.find()); print(matcher.group());
print(matcher.find());
print(matcher.find());
print("=====================");
// lookingAt 每次从头开始找
print(matcher.lookingAt());
print(matcher.lookingAt());
print(matcher.lookingAt());
}
2.2 find() - if/while
private static final String SUB_REGEX = "code\\d+";
private static final Pattern subSequenceCompile = Pattern.compile(SUB_REGEX);
@Test
public void subFind() {
String data = "code001,234code222,333code111,2323";
Matcher subMatcher = subSequenceCompile.matcher(data);
// group() 等价 group(0)
if (subMatcher.find()) {
System.out.println("if: " + subMatcher.group());
}
}
/**
* 注意此处的 If 与 While 的区别
* find() 是部分匹配
*/
@Test
public void subFind2() {
String data = "code001,234code222,333code111,2323";
Matcher subMatcher = subSequenceCompile.matcher(data);
while(subMatcher.find()) {
System.out.println("while: " + subMatcher.group());
}
}
2.3 start() / end()
@Test
public void startFind() {
String str = "123-4567-00";
Matcher m = pattern.matcher(str);
print(m.find() + "; " + m.start() + "-" + m.end());
print(m.find() + "; " + m.start() + "-" + m.end());
print(m.find());
// p(m.start() + "-" + m.end()); 找不到会报错
}
2.4 reset()
@Test
public void reset() {
String str = "123-3243-564-00";
Pattern pattern = Pattern.compile("\\d{3,5}");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());
// reset() 可以让 matcher.matches() 吃掉的字符吐出来
matcher.reset();
}
2.5 替换
@Test
public void replaceAll() {
String input = "java Java JaVA learnJAVA hellojAVA 2342342";
String regex = "java";
// Pattern.CASE_INSENSITIVE 不区分大小写
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
// print(matcher.group());
}
System.out.println(matcher.replaceAll("JAVA"));
}
升级:按单双数替换
private static final String REPLACE_REGEX = "java";
private static final Pattern replacePattern = Pattern.compile(REPLACE_REGEX, Pattern.CASE_INSENSITIVE);
/**
* 按单双数替换
*/
@Test
public void appendReplacement() {
Matcher matcher = replacePattern.matcher("java Java JaVA learnJAVA hellojAVA 2342342");
StringBuffer buffer = new StringBuffer();
int i = 0;
while (matcher.find()) {
i++;
if (i % 2 == 0) {
// 替换的结果放到 buffer
matcher.appendReplacement(buffer, "java");
} else {
matcher.appendReplacement(buffer, "JAVA");
}
}
// 把后面的添加到buffer
matcher.appendTail(buffer);
print(buffer);
}
网友评论