Java提供了java.util.regex
包,用於與正則運算式進行模式匹配。 Java正則運算式與Perl編程語言非常相似,非常容易學習。
正則運算式是一種特殊的字元序列,可使用模式中的專用語法來匹配或查找其他字串或字串集。 它們可用於搜索,編輯或操作文本和數據。
java.util.regex
包主要由以下三個類組成 -
Pattern
類 -Pattern
對象是正則運算式的編譯表示。Pattern
類不提供公共構造函數。 要創建模式,需要首先調用它的公共靜態compile()
方法,然後返回Pattern
對象。 這些方法接受正則運算式作為第一個參數。Matcher
類 -Matcher
對象是解釋模式並對輸入字串執行匹配操作的引擎。 與Pattern
類一樣,Matcher
沒有定義公共構造函數。 通過在Pattern
對象上調用matcher()
方法獲取Matcher
對象。PatternSyntaxException
-PatternSyntaxException
對象是未經檢查的異常,指示正則運算式模式中的語法錯誤。
1. 捕獲組
捕獲組是將多個字元視為一個單元的一種方法。 它們是通過將要分組的字元放在一組括弧中來創建的。 例如,正則運算式(dog
)創建包含字母d
,o
和g
的單個組。
捕獲組通過從左到右計算它們的左括弧來編號。 在運算式((A)(B(C)))
中,例如,有四個這樣的組 -
((A)(B(C)))
(A)
(B(C))
(C)
要查找運算式中存在多少個組,請在Matcher
對象上調用groupCount()
方法。 groupCount()
方法返回一個int
類型值,顯示Matcher
模式中存在的捕獲組數。
還有一個特殊組,即組0
,它始終代表整個運算式。 該組未包含在groupCount()
報告的總數中。
示例
以下示例說明如何從給定的字母數字字串中查找數字字串 -
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
}else {
System.out.println("NO MATCH");
}
}
}
執行上面示例代碼,得到以下結果:
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0
2. 正則運算式語法
下麵列出了Java中可用的所有正則運算式元字元語法 -
編號 | 子運算式 | 匹配 |
---|---|---|
1 | ^ |
匹配行的開頭。 |
2 | $ |
匹配行的結尾。 |
3 | . |
匹配除換行符之外的任何單個字元,使用m 選項也可以匹配換行符。 |
4 | [...] |
匹配括弧中的任何單個字元。 |
5 | [^...] |
匹配括弧內的任何單個字元。 |
6 | \A |
整個字串的開頭。 |
7 | \z |
整個字串的結尾。 |
8 | \Z |
除允許的最終行終止符之外的整個字串的結尾。 |
9 | re* |
匹配前面運算式的0 次或更多次出現。 |
10 | re+ |
匹配前面運算式的1 次或更多次出現。 |
11 | re? |
匹配前面運算式的0 或1 次出現。 |
12 | re{n} |
準確匹配前面運算式的n 次出現次數。 |
13 | re{n,} |
準確匹配前面運算式的n 次以上出現次數。 |
14 | aΙb | 匹配a 或b 。 |
15 | (re) |
對正則運算式進行分組並記住匹配的文本。 |
16 | (?: re) |
將正則運算式分組而不記住匹配的文本。 |
17 | (?> re) |
匹配獨立模式而無需回溯。 |
18 | \w |
匹配單詞字元。 |
19 | \W |
匹配非單詞字元。 |
20 | \s |
匹配空白符,相當於:[\t\n\r\f] |
21 | \S |
匹配非空白。 |
22 | \d |
匹配數字,相當於:[0-9] 。 |
23 | \D |
匹配非數字。 |
24 | \A |
匹配字串的開頭。 |
25 | \Z |
匹配字串的結尾。如果存在換行符,則它在換行符之前匹配。 |
26 | \z |
匹配字串的結尾。 |
27 | \G |
匹配最後一個匹配結束的點。 |
28 | \n |
反向引用以捕獲組號:n 。 |
29 | \b |
在括弧外部匹配單詞邊界,在括弧內匹配退格(0x08 )。 |
30 | \B |
匹配非字邊界。 |
31 | \n ,\t |
匹配換行符,回車符,跳位字元等。 |
32 | \E |
轉義(引用)所有字元直到\E 。 |
33 | \Q |
結束以\Q 開頭引用。 |
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); // get a matcher object
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
可以看到此示例使用單詞邊界來確保字母:c
,a
,t
不僅僅是較長單詞中的子字串。 它還提供了有關輸入字串中匹配發生位置的一些有用資訊。
start
方法返回上一個匹配操作期間給定組捕獲的子序列的起始索引,end
返回匹配的最後一個字元的索引加1
。
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 Pattern pattern;
private static Matcher matcher;
public static void main( String args[] ) {
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
}
}
執行上面示例代碼,得到以下結果:
Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
lookingAt(): true
matches(): false
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.
appendReplacement和appendTail方法
Matcher
類還提供了appendReplacement
和appendTail
方法來替換文本。
以下是上述方法的示例 -
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find()) {
m.appendReplacement(sb, REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}
執行上面示例代碼,得到以下結果:
-foo-foo-foo-