使用Matcher類,最重要的一個概念必須清楚:組(Group),在正則表達式中
()定義了一個組,由於一個正則表達式可以包含很多的組,所以下面先說說怎麼劃分組的,
以及這些組和組的下標怎麼對應的.
下面我們看看一個小例子,來說明這個問題
這個正則表達式有三個組:
整個\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));
- }
- }
- }
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 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
- }
- }
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() 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);
- }
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);
}
輸出結果:
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 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 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());
}
}
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 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);
- }
- }
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等進行查看和替換操作
- }
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
- }
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);
while(matcher.find()){
matcher.appendReplacement(sb,replacement);//結果是My name is Smith. James Smith
}
matcher.appendTail(sb);
的更便捷的方法。
- public String replaceFirst(String replacement)
public String replaceFirst(String replacement)
這個與replaceAll想對應很容易理解,就是隻替換第一個匹配的。