1. Java中的正則表達式應用
1、java.util.regex 包主要包括以下三個類:
-
Pattern 類:
pattern 對象是一個正則表達式的編譯表示。Pattern 類沒有公共構造方法。要創建一個 Pattern 對象,你必須首先調用其公共靜態編譯方法,它返回一個 Pattern 對象。該方法接受一個正則表達式作為它的第一個參數。 -
Matcher 類:
Matcher 對象是對輸入字符串進行解釋和匹配操作的引擎。與Pattern 類一樣,Matcher 也沒有公共構造方法。你需要調用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象。 -
PatternSyntaxException:
PatternSyntaxException 是一個非強制異常類,它表示一個正則表達式模式中的語法錯誤。
2. 下面介紹Pattern和Matcher 類中最常用的幾個方法
1、Pattern類
Pattern類中有兩個最常用的方法:
(1)boolean isMatch = Pattern.matches("regExp", "string");
matches()方法表示正則表達式regExp是否匹配字符串string,匹配返回true,不匹配返回false
注意:String類也有matches()方法,如"abcd".matches(regExp),其實他們倆是等價的,String類matches()方法就是調用的Pattern.matches()方法:
(2)Pattern pattern = Pattern.compile("regExp");
compile()方法表示編譯此正則表達式regExp,返回regExp被編譯后的pattern
如果只想知道該字符串是否匹配表達式,則直接使用matches()方法最簡單
2、Matcher類
Matcher類沒有提供什么靜態方法,通過調用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象,如
Pattern pattern = Pattern.compile("regExp");
Matcher matcher = pattern.matcher("string");
此時我們就得到了一個Matcher對象,通過此對象就可以對字符串進行操作了
Matcher類常用的方法:
(1)索引方法(精確表明輸入字符串中在哪能找到匹配)
- public int start() 返回以前匹配的初始索引
- public int end() 返回最后匹配字符之后的偏移量。
(2)研究方法(用來檢查輸入字符串並返回一個布爾值,表示是否找到該模式)
- public boolean lookingAt() 嘗試將從區域開頭開始的輸入序列與該模式匹配。
- public boolean find() 嘗試查找與該模式匹配的輸入序列的下一個子序列。
- public boolean matches() 嘗試將整個區域與模式匹配。
(3)替換方法(替換輸入字符串里文本的方法)
- public String replaceAll(String replacement) 替換模式與給定替換字符串相匹配的輸入序列的每個子序列。
- public String replaceFirst(String replacement) 替換模式與給定替換字符串匹配的輸入序列的第一個子序列。
3. 實例
1、start 和 end 方法
下面是一個對單詞 "cat" 出現在輸入字符串中出現次數進行計數的例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat";
public static void main( String args[] ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 獲取 matcher 對象
int count = 0;
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}
運行結果:
Match number 1
start(): 0
end(): 3
Match number 2
start(): 4
end(): 7
Match number 3
start(): 8
end(): 11
Match number 4
start(): 19
end(): 22
2、matches 和 lookingAt 方法
matches 和 lookingAt 方法都用來嘗試匹配一個輸入序列模式。它們的不同是 matches 要求整個序列都匹配,而lookingAt 不要求。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
private static Matcher matcher2;
public static void main( String args[] ){
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
matcher2 = pattern.matcher(INPUT2);
System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2);
System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
System.out.println("lookingAt(): "+matcher2.lookingAt());
}
}
運行結果:
Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
Current INPUT2 is: ooooofoooooooooooo
lookingAt(): true
matches(): false
lookingAt(): false
3、replaceFirst 和 replaceAll 方法
replaceFirst 和 replaceAll 方法用來替換匹配正則表達式的文本。不同的是,replaceFirst 替換首次匹配,replaceAll 替換所有匹配。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " +
"All dogs say meow.";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
運行結果:
The cat says meow. All cats say meow.