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));}}
}
输出结果: 引用
group 099SuperJava
group 1 :99
group 2 :SuperJava
group 1 :99
group 2 :SuperJava
下面我们看看Matcher类提供的方法:
public Pattern pattern()
这个方法返回了,创建Matcher的那个pattern对象。
下面我们看看一个小例子来说明这个结果
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()这个方法将Matcher的状态重新设置为最初的状态。
public Matcher reset(CharSequence input)
重新设置Matcher的状态,并且将候选字符序列设置为input后进行Matcher, 这个方法和重新创建一个Matcher一样,只是这样可以重用以前的对象。
public int start()
这个方法返回了,Matcher所匹配的字符串在整个字符串的的开始下标:
下面我们看看一个小例子
public class MatcherStartExample{public static void main(String args[]){test();}public static void test(){//create a Matcher and use the Matcher.start() methodString 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);
}
输出结果:
My name is Bond. James Bond.
^11
My name is Bond. James Bond.
^23
public int start(int group)
这个方法可以指定你感兴趣的sub group,然后返回sup group匹配的开始位置。
public int end()
这个和start()对应,返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量。
其实start和end经常是一起配合使用来返回匹配的子字符串。
public int end(int group)
和public int start(int group)对应,返回在sup group匹配的子字符串最后一个字符在整个字符串下标加一
public String group()
返回由以前匹配操作所匹配的输入子序列。
这个方法提供了强大而方便的工具,他可以等同使用start和end,然后对字符串作substring(start,end)操作。
看看下面一个小例子:
import java.util.regex.*;
public class MatcherGroupExample{public static void main(String args[]){test();}public static void test(){//create a PatternPattern p = Pattern.compile("Bond");//create a Matcher and use the Matcher.group() methodString candidateString = "My name is Bond. James Bond.";Matcher matcher = p.matcher(candidateString);//extract the groupmatcher.find();System.out.println(matcher.group());}
}
public String group(int group)
这个方法提供了强大而方便的工具,可以得到指定的group所匹配的输入字符串
因为这两个方法经常使用,同样我们看一个小例子:
import java.util.regex.*;
public class MatcherGroupParamExample{public static void main(String args[]){test();}public static void test(){//create a PatternPattern p = Pattern.compile("B(ond)");//create a Matcher and use the Matcher.group(int) methodString candidateString = "My name is Bond. James Bond.";//create a helpful index for the sake of outputMatcher matcher = p.matcher(candidateString);//Find group number 0 of the first findmatcher.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 findmatcher.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)从输入字符串指定的start位置开始查找。
public boolean lookingAt()
基本上是matches更松约束的一个方法,尝试将从区域开头开始的输入序列与该模式匹配
public Matcher appendReplacement (StringBuffer sb, String replacement)
你想把My name is Bond. James Bond. I would like a martini中的Bond换成Smith
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来替换所有的匹配。 public StringBuffer appendTail(StringBuffer sb)
这个方法简单的把为匹配的结尾追加到StringBuffer中。在上一个例子的最后再加上一句:
matcher.appendTail(sb);
结果就会成为My name is Smith. James Smith. I would like a martini.
public String replaceAll(String replacement)
这个是一个更方便的方法,如果我们想替换所有的匹配的话,我们可以简单的使用replaceAll就ok了。
是:
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