Java正規表達式 & Java方法
Java正規表達式
正規表達式定義了字元串的模式
正規表達式可以用來搜尋、編輯或者處理文本
正規表達式并不僅限于某一種語言,但是每種語言都有細微的差别
Java的正規表達式與Perl最為相似:
java.util.regex包主要包括以下三個類:
- Pattern類:
pattern 對象是一個正規表達式的編譯表示。Pattern 類沒有公共構造方法。要建立一個 Pattern 對象,你必須首先調用其公共靜 态編譯方法,它傳回一個 Pattern 對象。該方法接受一個正規表達式作為它的第一個參數
- Matcher類:
Matcher 對象是對輸入字元串進行解釋和比對操作的引擎。與Pattern 類一樣,Matcher 也沒有公共構造方法。你需要調用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象
- PatternSyntaxException:
PatternSyntaxException 是一個非強制異常類,它表示一個正規表達式模式中的文法錯誤。
import java.util.regex.*;
public class RegexTest{
public static void main(String[] args)
{
String content = "I am very beautiful!";
String pattern = ".*very.*";//是否包含very
boolean isMatch = Pattern.matches(pattern,content);
System.out.println("字元串是否包含very?" + isMatch);
}
}
//結果為:字元串是否包含very?true
捕獲組
捕獲組是把多個字元當一個單獨單元進行處理的方法,它通過對括号内的字元分組來建立。
捕獲組是通過從左至右計算其開括号來編号。例如,在表達式((A)(B(C)))有四個這樣的組:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
- 通過調用 matcher 對象的 groupCount 方法來檢視表達式有多少個分組。groupCount 方法傳回一個 int 值,表示matcher對象目前有多個捕獲組
- 還有一個特殊的組(group(0)),它總是代表整個表達式。該組不包括在 groupCount 的傳回值中
正規表達式文法
- 在其他語言中,\\ 表示:想要在正規表達式中插入一個普通的(字面上的)反斜杠,請不要給它任何特殊的意義。
- 在 Java 中,\\ 表示:要插入一個正規表達式的反斜線,是以其後的字元具有特殊的意義。
是以,在 Java 中正規表達式中則需要有兩個反斜杠才能被解析為其他語言中的轉義作用。也就是說兩個 \\ 代表其他語言中的一個 \,這也就是為什麼表示一位數字的正規表達式是 \\d,而表示一個普通的反斜杠是 \\\\
Matcher 類的方法
序号 | 方法及說明 |
---|---|
1 | public int start() 傳回以前比對的初始索引 |
2 | public int start(int group) 傳回在以前的比對操作期間,由給定組所捕獲的子序列的初始索引 |
3 | public int end() 傳回最後比對字元之後的偏移量 |
4 | public int end(int group) 傳回在以前的比對操作期間,由給定組所捕獲子序列的最後字元之後的偏移量 |
5 | public boolean lookingAt() 嘗試将從區域開頭開始的輸入序列與該模式比對 |
6 | public boolean find() 嘗試查找與該模式比對的輸入序列的下一個子序列 |
7 | public boolean find(int start) 重置此比對器,然後嘗試查找比對該模式、從指定索引開始的輸入序列的下一個子序列 |
8 | public boolean matches() 嘗試将整個區域與模式比對 |
9 | public Matcher appendReplacement(StringBuffer sb, String replacement) 實作非終端添加和替換步驟 |
10 | public StringBuffer appendTail(StringBuffer sb) 實作終端添加和替換步驟 |
11 | public String replaceAll(String replacement) 替換模式與給定替換字元串相比對的輸入序列的每個子序列 |
12 | public String replaceFirst(String replacement) 替換模式與給定替換字元串比對的輸入序列的第一個子序列 |
13 | public static String quoteReplacement(String s) 傳回指定字元串的字面替換字元串。這個方法傳回一個字元串,就像傳遞給Matcher類的appendReplacement 方法一個字面字元串一樣工作。 |
PatternSyntaxException 類的方法
- PatternSyntaxException 是一個非強制異常類,它訓示一個正規表達式模式中的文法錯誤
- PatternSyntaxException 類提供了下面的方法
序号 | 方法與說明 |
---|---|
1 | public String getDescription() 擷取錯誤的描述 |
2 | public int getIndex() 擷取錯誤的索引 |
3 | public String getPattern() 擷取錯誤的正規表達式模式 |
4 | public String getMessage() 傳回多行字元串,包含文法錯誤及其索引的描述、錯誤的正規表達式模式和模式中錯誤索引的可視化訓示 |
舉例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
private static final String REGEX = "\\bdog\\b";
private static final String REGEX2 = "cat";
private static final String INPUT = "dogcat cat dogtie cat";
private static final String INPUT2 = "cat dog dog cattie cat";
public static void TestStartEnd (){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 擷取 matcher 對象
Matcher m2 = p.matcher(INPUT2); // 擷取 matcher 對象
int count = 0;
int count2=0;
printTest(m,count);
printTest(m2,count2);
Pattern p2 = Pattern.compile(REGEX2);
Matcher m3 = p2.matcher(INPUT); // 擷取 matcher 對象
Matcher m4 = p2.matcher(INPUT2); // 擷取 matcher 對象
System.out.println("Current REGEX2 is: "+REGEX2);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2);
System.out.println("lookingAt(): "+m3.lookingAt());
System.out.println("matches(): "+m3.matches());
System.out.println("lookingAt(): "+m4.lookingAt());
}
private static void printTest(Matcher m,int count)
{
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}
Java學習之七 Java日期時間