Pattern類:
static
Pattern compile(String regex) :將給定的正則表達式編譯並賦予給Pattern類
String[]
split(CharSequence input) :將目標字符串按照Pattern裏所包含的正則表達式爲模進行分割。
String[] split(CharSequence input, int limit) :作用同上,增加參數limit目的在於要指定分割的段數,如將limi設爲2,那麼目標字符串將根據正則表達式分爲割爲兩段。
一個正則表達式,也就是一串有特定意義的字符,必須首先要編譯成爲一個Pattern類的實例,這個Pattern對象將會使用matcher()方法來生成一個Matcher實例,接着便可以使用該
Matcher實例以編譯的正則表達式爲基礎對目標字符串進行匹配工作,多個Matcher是可以共用一個Pattern對象的。
例子:
public class Replacement{
public static void main(String[] args) throws Exception {
// 生成一個Pattern,同時編譯一個正則表達式
Pattern p = Pattern.compile("[/]+");
//用Pattern的split()方法把字符串按"/"分割
String[] result = p.split(
"Kevin has seen《LEON》seveal times,because it is a good film."
+"/ 凱文已經看過《這個殺手不太冷》幾次了,因爲它是一部"
+"好電影。/名詞:凱文。");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
輸出結果爲:
Kevin has seen《LEON》seveal times,because it is a good film.
凱文已經看過《這個殺手不太冷》幾次了,因爲它是一部好電影。
名詞:凱文。
很明顯,該程序將字符串按"/"進行了分段,我們以下再使用
split(CharSequence input, int limit)方法來指定分段的段數,程序改動爲:
tring[] result = p.split("Kevin has seen《LEON》seveal times,because it is a good film./ 凱文已經看過《這個殺手不太冷》幾次了,因爲它是一部好電影。/名詞:凱文。",2);
這裏面的參數"2"表明將目標語句分爲兩段。
輸出結果則爲:
Kevin has seen《LEON》seveal times,because it is a good film.
凱文已經看過《這個殺手不太冷》幾次了,因爲它是一部好電影。/名詞:凱文。
Matcher類:
1.一個Matcher對象是由一個Pattern對象調用其matcher()方法而生成的,一旦該Matcher對象生成,它就可以進行三種不同的匹配查找操作:
matches()方法嘗試對整個目標字符展開匹配檢測,也就是隻有整個目標字符串完全匹配時才返回真值。lookingAt ()方法將檢測目標字符串是否以匹配的子串起始。
find()方法嘗試在目標字符串裏查找下一個匹配子串。
以上三個方法都將返回一個布爾值來表明成功與否。
2.Matcher類同時提供了四個將匹配子串替換成指定字符串的方法:
replaceAll() :將目標字符串裏與既有模式相匹配的子串全部替換爲指定的字符串。
replaceFirst() :將目標字符串裏第一個與既有模式相匹配的子串替換爲指定的字符串。
appendReplacement() :將當前匹配子串替換爲指定字符串,並且將替換後的子串以及其之前到上次匹配子串之後的字符串段添加到一個StringBuffer對象裏。
appendTail() 將最後一次匹配工作後剩餘的字符串添加到一個StringBuffer對象裏。
例子:有字符串fatcatfatcatfat,假設既有正則表達式模式爲"cat",第一次匹配後調用appendReplacement(sb,"dog"),那麼這時StringBuffer sb的內容爲fatdog,也就是fatcat中的cat被替換爲dog並且與匹配子串前的內容加到sb裏,而第二次匹配後調用appendReplacement(sb,"dog"),那麼sb的內容就變爲fatdogfatdog,如果最後再調用一次appendTail(sb),那麼sb最終的內容將是fatdogfatdogfat。
例程://該例將把句子裏的"Kelvin"改爲"Kevin"
import java.util.regex.*;
public class MatcherTest{
public static void main(String[] args)
throws Exception {
//生成Pattern對象並且編譯一個簡單的正則表達式"Kelvin"
Pattern p = Pattern.compile("Kelvin");
//用Pattern類的matcher()方法生成一個Matcher對象
Matcher m = p.matcher("Kelvin Li and Kelvin Chan are both working in Kelvin Chen's KelvinSoftShop company");
StringBuffer sb = new StringBuffer();
int i=0;
//使用find()方法查找第一個匹配的對象
boolean result = m.find();
//使用循環將句子裏所有的kelvin找出並替換再將內容加到sb裏
while(result) {
i++;
m.appendReplacement(sb, "Kevin");
System.out.println("第"+i+"次匹配後sb的內容是:"+sb);
//繼續查找下一個匹配對象
result = m.find();
}
//最後調用appendTail()方法將最後一次匹配後的剩餘字符串加到sb裏;
m.appendTail(sb);
System.out.println("調用m.appendTail(sb)後sb的最終內容是:"+ sb.toString());
}
}
最終輸出結果爲:
第1次匹配後sb的內容是:Kevin
第2次匹配後sb的內容是:Kevin Li and Kevin
第3次匹配後sb的內容是:Kevin Li and Kevin Chan are both working in Kevin
第4次匹配後sb的內容是:Kevin Li and Kevin Chan are both working in Kevin Chen's Kevin
調用m.appendTail(sb)後sb的最終內容是:Kevin Li and Kevin Chan are both working in Kevin Chen's KevinSoftShop company.