Matcher类:
\w(\d\d)(\w+)
这个正则表达式有三个组: 整个\w(\d\d)(\w+) 是第0组 group(0) (\d\d)是第1组 group(1) (\w+)是第2组 group(2) 我们看看和正则表达式匹配的一个字符串x99SuperJava, group(0)是匹配整个表达式的字符串的那部分x99SuperJava group(1)是第1组(\d\d)匹配的部分:99 group(2)是第二组(\w+)匹配的那部分SuperJava |
|
package edu.jlu.fuliang;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String[] args) {
String regex = "\\w(\\d\\d)(\\w+)";
String candidate = "x99SuperJava";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(candidate);
if(matcher.find()){
int gc = matcher.groupCount();
for(int i = 0; i <= gc; i++)
System.out.println("group " + i + " :" + matcher.group(i));
}
}
}
输出结果:
import java.util.regex.*;
public class MatcherPatternExample{
public static void main(String args[]){
test();
}
public static void test(){
Pattern p = Pattern.compile("\\d");
Matcher m1 = p.matcher("55");
Matcher m2 = p.matcher("fdshfdgdfh");
System.out.println(m1.pattern() == m2.pattern());
//return true
}
}
public Matcher reset()
public class MatcherStartExample{
public static void main(String args[]){
test();
}
public static void test(){
//create a Matcher and use the Matcher.start() method
String candidateString = "My name is Bond. James Bond.";
String matchHelper[] =
{" ^"," ^"};
Pattern p = Pattern.compile("Bond");
Matcher matcher = p.matcher(candidateString);
//Find the starting point of the first 'Bond'
matcher.find();
int startIndex = matcher.start();
System.out.println(candidateString);
System.out.println(matchHelper[0] + startIndex);
//Find the starting point of the second 'Bond'
matcher.find();
int nextIndex = matcher.start();
System.out.println(candidateString);
System.out.println(matchHelper[1] + nextIndex);
}
import java.util.regex.*;
public class MatcherGroupExample{
public static void main(String args[]){
test();
}
public static void test(){
//create a Pattern
Pattern p = Pattern.compile("Bond");
//create a Matcher and use the Matcher.group() method
String candidateString = "My name is Bond. James Bond.";
Matcher matcher = p.matcher(candidateString);
//extract the group
matcher.find();
System.out.println(matcher.group());
}
}
import java.util.regex.*;
public class MatcherGroupParamExample{
public static void main(String args[]){
test();
}
public static void test(){
//create a Pattern
Pattern p = Pattern.compile("B(ond)");
//create a Matcher and use the Matcher.group(int) method
String candidateString = "My name is Bond. James Bond.";
//create a helpful index for the sake of output
Matcher matcher = p.matcher(candidateString);
//Find group number 0 of the first find
matcher.find();
String group_0 = matcher.group(0);
String group_1 = matcher.group(1);
System.out.println("Group 0 " + group_0);
System.out.println("Group 1 " + group_1);
System.out.println(candidateString);
//Find group number 1 of the second find
matcher.find();
group_0 = matcher.group(0);
group_1 = matcher.group(1);
System.out.println("Group 0 " + group_0);
System.out.println("Group 1 " + group_1);
System.out.println(candidateString);
}
}
public int groupCount()
这个方法返回了,正则表达式的匹配的组数。
public boolean matches()
尝试将整个区域与模式匹配。这个要求整个输入字符串都要和正则表达式匹配。
和find不同, find是会在整个输入字符串查找匹配的子字符串。
public boolean find()
find会在整个输入中寻找是否有匹配的子字符串,一般我们使用find的流程:
while(matcher.find()){
//在匹配的区域,使用group,replace等进行查看和替换操作
}
public boolean find(int start)
StringBuffer sb = new StringBuffer();
String replacement = "Smith";
Pattern pattern = Pattern.compile("Bond");
Matcher matcher =pattern.matcher("My name is Bond. James Bond. I would like a martini.");
while(matcher.find()){
matcher.appendReplacement(sb,replacement);//结果是My name is Smith. James Smith
}
Matcher对象会维护追加的位置,所以我们才能不断地使用appendReplacement来替换所有的匹配。
while(matcher.find()){
matcher.appendReplacement(sb,replacement);//结果是My name is Smith. James Smith
}
matcher.appendTail(sb);
的更便捷的方法。
public String replaceFirst(String replacement)
这个与replaceAll想对应很容易理解,就是只替换第一个匹配的。
转载于:https://blog.51cto.com/375163948/1342448