Java 正則表達式的使用


Java 正則表達式的使用

java.util.regex 包主要包括以下三個類:

Pattern 類:

  pattern 對象是一個正則表達式的編譯表示。Pattern 類沒有公共構造方法。要創建一個 Pattern 對象,你必須首先調用其公共靜態編譯方法,它返回一個 Pattern 對象。該方法接受一個正則表達式作為它的第一個參數。

Matcher 類:

  Matcher 對象是對輸入字符串進行解釋和匹配操作的引擎。與Pattern 類一樣,Matcher 也沒有公共構造方法。你需要調用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象。

PatternSyntaxException

  PatternSyntaxException 是一個非強制異常類,它表示一個正則表達式模式中的語法錯誤。

  以下實例中使用了正則表達式.*runoob.*用於查找字符串中是否包含了runoob子串:

 1 package cc.bcy;
 2 
 3 import java.util.regex.*;
 4 
 5 public class RegexExample 
 6 {
 7     public static void main(String[] args) 
 8     {
 9         String content="I am noob from runoob.com";
10         String pattern=".*runoob.*";
11         boolean isMatch=Pattern.matches(pattern, content);
12         System.out.println("字符串中是否包含了‘runoob’子字符串?"+isMatch);
13     }
14 }
15 /*
16 字符串中是否包含了‘runoob’子字符串?true
17 */

捕獲組:

  捕獲組是把多個字符當一個單獨單元進行處理的方法,它通過對括號內的字符分組來創建。可以通過調用 matcher 對象的 groupCount 方法來查看表達式有多少個分組。groupCount 方法返回一個 int 值,表示matcher對象當前有多個捕獲組。

  還有一個特殊的組(group(0)),它總是代表整個表達式。該組不包括在 groupCount 的返回值中。

 

 1 package cc.bcy;
 2 
 3 import java.util.regex.*;
 4 
 5 public class RegexExample 
 6 {
 7     public static void main(String[] args) 
 8     {
 9         String line="This order was placed for QT3000! OK?";
10         String pattern="(\\D*)(\\d+)(.*)";
11         //創建Pattern對象
12         Pattern p=Pattern.compile(pattern);
13         //創建Matcher對象
14         Matcher m=p.matcher(line);
15         if(m.find())
16         {
17             System.out.println("Found value: "+m.group(0));
18             System.out.println("Found value: "+m.group(1));
19             System.out.println("Found value: "+m.group(2));
20             System.out.println("Found value: "+m.group(3));
21         }
22         else
23         {
24             System.out.println("No Match!");
25         }
26         int n=m.groupCount();
27         System.out.println("一共有"+n+"個捕獲組");
28     }
29 }
30 /*
31 Found value: This order was placed for QT3000! OK?
32 Found value: This order was placed for QT
33 Found value: 3000
34 Found value: ! OK?
35 一共有3個捕獲組
36 */

Java 正則表達式語法:

  在其他語言中,\\ 表示:我想要在正則表達式中插入一個普通的(字面上的)反斜杠,請不要給它任何特殊的意義。 

  在 Java 中,\\ 表示:我要插入一個正則表達式的反斜線,所以其后的字符具有特殊的意義。

  所以,在其他的語言中(如Perl),一個反斜杠 \ 就足以具有轉義的作用,而在 Java 中正則表達式中則需要有兩個反斜杠才能被解析為其他語言中的轉義作用。也可以簡單的理解在 Java 的正則表達式中,兩個 \\ 代表其他語言中的一個 \,這也就是為什么表示一位數字的正則表達式是 \\d,而表示一個普通的反斜杠是 \\\\

 

Matcher 類的方法

  

下面是一個對單詞 "cat" 出現在輸入字符串中出現次數進行計數的例子:

 

 1 package cc.bcy;
 2 
 3 import java.util.regex.*;
 4 
 5 public class RegexExample 
 6 {
 7     private static final String REGEX="\\bcat\\b";
 8     private static final String INPUT="cat cat cat cattie cat";
 9     public static void main(String[] args) 
10     {
11         Pattern p=Pattern.compile(REGEX);
12         Matcher m=p.matcher(INPUT);
13         int count=0;
14         while(m.find())
15         {
16             count++;
17             System.out.println("Match number "+count);
18             System.out.println("start() "+m.start());
19             System.out.println("end() "+m.end());
20             System.out.println();
21         }
22     }
23 }
24 /*
25 Match number 1
26 start() 0
27 end() 3
28 
29 Match number 2
30 start() 4
31 end() 7
32 
33 Match number 3
34 start() 8
35 end() 11
36 
37 Match number 4
38 start() 19
39 end() 22
40 */

 

  matches lookingAt 方法都用來嘗試匹配一個輸入序列模式。它們的不同是 matches 要求整個序列都匹配,而lookingAt 不要求。lookingAt 方法雖然不需要整句都匹配,但是需要從第一個字符開始匹配。

  

  把字符串中的匹配項全部替換成-”:(用appendReplacement()方法)

 1 package cc.bcy;
 2 
 3 import java.util.regex.*;
 4 
 5 public class RegexExample 
 6 {
 7     private static final String REGEX="a*b";
 8     private static final String INPUT="aabfooaabfooabfoobkkk";
 9     public static void main(String[] args) 
10     {
11         Pattern p=Pattern.compile(REGEX);
12         Matcher m=p.matcher(INPUT);
13         StringBuffer sb=new StringBuffer();
14         while(m.find())
15         {
16             m.appendReplacement(sb, "-");
17         }
18         m.appendTail(sb);
19         System.out.println(sb.toString());
20     }
21 }
22 /*
23 -foo-foo-foo-kkk
24 */

  replaceFirst replaceAll 方法用來替換匹配正則表達式的文本。不同的是,replaceFirst 替換首次匹配,replaceAll 替換所有匹配。

 1 package cc.bcy;
 2 
 3 import java.util.regex.*;
 4 
 5 public class RegexExample 
 6 {
 7     private static final String REGEX="dog";
 8     private static final String INPUT="The dog says meow. All dogs say meow.";
 9     public static void main(String[] args) 
10     {
11         Pattern p=Pattern.compile(REGEX);
12         Matcher m=p.matcher(INPUT);
13         String str=m.replaceAll("cat");
14         System.out.println(str);
15     }
16 }
17 /*
18 The cat says meow. All cats say meow.
19 */

PatternSyntaxException 類的方法:

  PatternSyntaxException 是一個非強制異常類,它指示一個正則表達式模式中的語法錯誤。 PatternSyntaxException 類提供了下面的方法來幫助我們查看發生了什么錯誤。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM