1. find()
- 每调用一次 find() 方法,如果返回 true,就表示“找到一个匹配”
public class RegexDemo {
private static final String regex = "\\d{3,5}";
private static final Pattern pattern = Pattern.compile(regex);
@Test
public void find() {
String str = "123-4567-00";
Matcher matcher = pattern.matcher(str);
// 找子串,找到一个子串,然后把该子串去掉
print(matcher.find());
print(matcher.find());
print(matcher.find());
print("=====================");
// lookingAt 每次从头开始找
print(matcher.lookingAt());
print(matcher.lookingAt());
print(matcher.lookingAt());
}
public static void print(Object o) {
System.out.println(o);
}
}
1.1 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()); 找不到会报错
}
3. group()
3.1 分组
- 分组由小括号组成,一个小括号是一组
- 多层嵌套:左小括号开始,第一个左括号是第一组,第二个是第二组
示例:
regex = "\\d{3,5}[a-z]{2}" # 不加小括号
regex = "(\\d{3,5})([a-z]{2})"
3.2 group(0)
- matcher.group() 等价 matcher.group(0)
- Matcher.group(0) 表示正则表达式中符合条件的字符串。(整个匹配)
- Matcher.group(1) 表示正则表达式中符合条件的字符串中的第一个()中的字符串
// java.util.regex.Matcher#group()
public String group() {
return group(0);
}
@Test
public void group() {
Pattern pattern = Pattern.compile("(\\d{3,5})([a-z]{2})");
Matcher matcher = pattern.matcher("123aa-23232re-3434");
while (matcher.find()) {
print(matcher.group());
print(matcher.group(1) + "###" + matcher.group(2));
}
}
@Test
public void group2() {
Pattern pattern = Pattern.compile("\\d{3,5}[a-z]{2}");
Matcher matcher = pattern.matcher("123aa-23232re-3434");
while (matcher.find()) {
print("group(): " + matcher.group());
print("group(0): " + matcher.group(0));
}
}
3.3 替换
@Test
public void test() {
// CASE_INSENSITIVE: Enables case-insensitive matching.
Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("java Java JaVA learnJAVA hellojAVA 2342342");
// while (matcher.find()) {
// print(matcher.group());
// }
System.out.println(matcher.replaceAll("JAVA"));
}
private static final String regex = "java";
private static final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
@Test
public void replaceMent() {
Matcher matcher = pattern.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);
}
@Test
public void test() {
String data = "wer1231xiao001,23423xiao222,333xing11,2323";
// 是否匹配子串
// subSequenceCompile.matcher(data).find() -- boolean
// Pattern.matches(ENTIRE_REGEX, data); 等价 entireCompile.matcher(data).matches()
Matcher subMatcher = subSequenceCompile.matcher(data);
while(subMatcher.find()) {
System.out.println(subMatcher.group()); // 等价 subMatcher.group(0);
}
Matcher groupMather = groupCompile.matcher(data);
while(groupMather.find()) {
System.out.println(groupMather.groupCount());
// xiao001,23423xiao222,333xing11; group1:xiao001; group2:xing11
System.out.println(groupMather.group() + "; group1:" + groupMather.group(1) + "; group2:" + groupMather.group(2));
}
}
网友评论