1. 程式人生 > >JAVA中正則表達式學習總結

JAVA中正則表達式學習總結

等價 regex 5.1 表示 最好 方法 system lin 劃線

一、JAVA中正則表達式相關的類

1. java.util.regex.Pattern

  該類用於編譯模式,模式可以理解為一個正則表達式,比如:a*b。

  用法如下:

  // 創建模式

    Pattern p = Pattern.compile("a*b");

    // 創建該模式的匹配器

   Matcher m = p.matcher("aaaaab");

2. java.util.regex.Matcher

  該類用於創建匹配器和進行匹配操作。

  用法如下:

  // 進行匹配操作

  boolean b = m.matches();

3. 運行一個正則表達式入門案例

public class C {
    public static void main(String[] args) {
        new C().test();
    }    
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("a*b");
        // 創建該模式的匹配器
        Matcher m = p.matcher("aaaaab");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

二、正則表達式基礎

1. 純文本也是一個正則表達式

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("ab");
        // 創建該模式的匹配器
        Matcher m = p.matcher("ab");
        // 進行匹配操作
        boolean
b = m.matches(); System.out.println(b); } }

運行結果:true;

2. 字符可以匹配任意單個的字符、字母、數字。

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile(".b");
        // 創建該模式的匹配器
        Matcher m = p.matcher("ab");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

3. 用[ ]匹配一組字符

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[0123456789]");
        // 創建該模式的匹配器,5和[0123456789]中的某一個匹配
        Matcher m = p.matcher("5");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

4. 用-簡化[ ]中元素的列舉

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[0-9]");
        // 創建該模式的匹配器
        Matcher m = p.matcher("5");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

類似用法:[a-z]、[A-Z]、[a-zA-Z]、[a-zA-Z0-9]

註意:類似[z-a]的是非法的regex。

-只有在[ ]中才是元字符,如果在[ ]就是一個普通字符。

5. ^取非操作符

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[^0-9a]");
        // 創建該模式的匹配器,註意^取非是集合[]中所有元素取非,不只是0-9
        Matcher m = p.matcher("a");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:false

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[^0-9]");
        // 創建該模式的匹配器
        Matcher m = p.matcher("a");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

6. 匹配數字的元字符:\d 等價於[0-9]

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式,註意在正則表達式中\也需要轉義即:\\
        Pattern p = Pattern.compile("\\d");
        // 創建該模式的匹配器
        Matcher m = p.matcher("4");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

7. 匹配非數字的元字符:\D相當於[^0-9]

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\D");
        // 創建該模式的匹配器
        Matcher m = p.matcher("4");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:false

8. \w:匹配任何一個字母數字字符(大小寫均可)或下劃線字符(等價於[a-zA-Z0-9_])

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\w");
        // 創建該模式的匹配器
        Matcher m = p.matcher("_");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

9. 匹配任何一個非字母數字或非下劃線字符(等價於[^a-zA-Z0-9_])

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\W");
        // 創建該模式的匹配器
        Matcher m = p.matcher("_");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

10. +匹配一個或多個(至少一個)。+遇到[ ]一般放在[ ]後,如果放在[ ]中就只是一個元素

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 +可以重復一個集合[]也可以是一個字符
        Pattern p = Pattern.compile("[abc]+");
        // 創建該模式的匹配器
        Matcher m = p.matcher("aaccbbabc");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d+");
        // 創建該模式的匹配器
        Matcher m = p.matcher("1114578");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

11. *匹配零個或多個

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d*");
        // 創建該模式的匹配器
        Matcher m = p.matcher("");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

12. ?匹配零個或一個(最多一個)

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d?");
        // 創建該模式的匹配器
        Matcher m = p.matcher("");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d?");
        // 創建該模式的匹配器
        Matcher m = p.matcher("2");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d?");
        // 創建該模式的匹配器
        Matcher m = p.matcher("25");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:false

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[abc]?");
        // 創建該模式的匹配器
        Matcher m = p.matcher("c");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

13. { }指定重復匹配的次數

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式  {2}表示匹配兩次
        Pattern p = Pattern.compile("[abc]{2}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("cc");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 {2,5}最少匹配2次,最多匹配5次
        Pattern p = Pattern.compile("[abc]{2,5}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("ccab");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

public class C {
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 最好重復匹配3次
        Pattern p = Pattern.compile("[abc]{3,}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("ccabbbbbbbbbbbbbb");
        // 進行匹配操作
        boolean b = m.matches();
        System.out.println(b);
    }
}

運行結果:true

14. ( )中的|表示“或”

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("t(a|e|i|o|oo)n");
        // 輸出匹配結果
        System.out.println(p.matcher("tan").matches());
        System.out.println(p.matcher("ten").matches());
        System.out.println(p.matcher("tin").matches());
        System.out.println(p.matcher("ton").matches());
        //[ ]中只能匹配一個字符,而( )中可以不止一個字符,如“oo”
        System.out.println(p.matcher("toon").matches());
    }
}

運行結果:

true

true

true

true

true

當一個模式使用多次時,以下寫法會編譯多次,會影響性能.

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        //如果要多次使用一種模式,編譯一次後重用此模式比每次都調用此方法效率更高。
        System.out.println(Pattern.matches("t(a|e|i|o|oo)n", "toon"));
    }
}    
運行結果:true

15. \b指定單詞邊界,匹配的長度可以認為為“0”。b是英文boundary(邊界)的首字母。

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 
        Pattern p = Pattern.compile("\\bis\\b");
        // 創建該模式的匹配器
        Matcher m = p.matcher("This is a dog,that is a cat");
        // 進行匹配操作
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

is

is

技術分享

有4種位置被認為是“單詞邊界”:

1)在字符串的第一個字符前的位置(如果字符串的第一個字符是一個“單詞字符”)

2)在字符串的最後一個字符後的位置(如果字符串的最後一個字符是一個“單詞字符”)

3)在一個“單詞字符”和“非單詞字符”之間,其中“非單詞字符”緊跟在“單詞字符”之後

4)在一個“非單詞字符”和“單詞字符”之間,其中“單詞字符”緊跟在“非單詞字符”後面“單詞字符”是可以用“\w”匹配的字符,“非單詞字符”是可以用“\W”匹配的字符。

正則表達式匹配單詞的原理探討:

讓我們看看把正則表達式<<\bis\b>>應用到字符串“This island is beautiful”。引擎先處理符號<<\b>>。因為\b是0長度 ,所以第一個字符T前面的位置會被考察。

因為T是一個“單詞字符”,而它前面的字符是一個空字符(void),所以\b匹配了單詞邊界。接著<<i>>和第一個字符“T”匹配失敗。

匹配過程繼續進行,直到第五個空格符,和第四個字符“s”之間又匹配了<<\b>>。然而空格符和<<i>>不匹配。

繼續向後,到了第六個字符“i”,和第五個空格字符之間匹配了<<\b>>,然後<<is>>和第六、第七個字符都匹配了。

然而第八個字符和第二個“單詞邊界”不匹配,所以匹配又失敗了。到了第13個字符i,因為和前面一個空格符形成“單詞邊界”,同時<<is>>和“is”匹配。

引擎接著嘗試匹配第二個<<\b>>。因為第15個空格符和“s”形成單詞邊界,所以匹配成功。引擎“急著”返回成功匹配的結果。

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 ,匹配以th或Th開頭的單詞(註意:模式默認是區分大小寫的)
        Pattern p = Pattern.compile("(\\bth\\w+|\\bTh\\w+)");
        // 創建該模式的匹配器
        Matcher m = p.matcher("This is a dog,that is a cat");
        // 進行匹配操作
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

This

that

16. 字符串邊界

用來定義字符串邊界的元字符有兩個:一個是用來定義字符串開頭的^,另一個是用來定義字符串結尾的$。

技術分享

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式 匹配一個字符串
        Pattern p = Pattern.compile("^this\\s+a\\s+dog$");
        // 創建該模式的匹配器
        Matcher m = p.matcher("this   a   dog");
        // 進行匹配操作,並輸出匹配結果
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

this a dog

17. 註意:重復匹配的元字符(*,?,{3},+)只作用於緊挨著她的的前一個字符或元字符或字表達式。

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("https?");
        // 創建該模式的匹配器
        Matcher m = p.matcher("http");
        //輸出匹配的字符串
        while(m.find()){
             System.out.println(m.group());
        }
    }
}

運行結果:

http

18. ()子表達式。子表達式用()包裹起來,可以理解為包含其他元字符的一個元字符。

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("Pinging baidu.com [12.159.46.200] with 32 bytes of data");
        //輸出匹配的字符串
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

12.159.46.200

ublic class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式,匹配ip。把上面重復的\\d{1,3}\\.可以認為是一個字表達式
        Pattern p = Pattern.compile("(\\d{1,3}\\.){3}\\d{1,3}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("Pinging baidu.com [12.159.46.200] with 32 bytes of data;");
        //輸出匹配的字符串
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

12.159.46.200

19. 子表達式的嵌套

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式,匹配19或20開頭的年份
        Pattern p = Pattern.compile("(19|20)\\d{2}");
        // 創建該模式的匹配器
        Matcher m = p.matcher("today is 2014-4-7");
        //輸出匹配的字符串
        while(m.find()){
             System.out.println(m.group());
        }
    }
}    

運行結果:

2014

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式,匹配一個合法IP(最大只能是255)
        Pattern p = Pattern.compile("((\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}((\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))");
        // 創建該模式的匹配器
        Matcher m = p.matcher("IP: 265.255.10.0");
        System.out.println(m.matches());
    }
}    

運行結果:

false

三、常用實例

1. 只匹配中文

public class C { 
    public static void main(String[] args) {
        new C().test();
    }
    public void test(){
        // 創建模式
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        // 創建該模式的匹配器
        Matcher m = p.matcher("china中國");
        //輸出匹配的字符串
        while(m.find()){
             System.out.println(m.group());
        }
    }
}

運行結果:

JAVA中正則表達式學習總結