正則表達式小結

又認真看了一次正則表達式的相關的內容,發現,好記性不如爛筆頭,古人誠不欺我也,所以寫下這篇博文,一是爲了跟大家分享一下,第二,方便那天我忘記正則表達式的內容了,就回頭查看一下...如果有什麼不恰當的地方,望瀏覽者能夠大方指出,便於相互學習。



\\d 表示的是一個數字 \\ .表示一個小數點

反斜線字符 ('\') 用於引用轉義構造,同時還用於引用其他將被解釋爲非轉義構造的字符。

[a-z]{3}表示三個a~z的字母           X   表示一個字母          . 表示一個字符

. 表示一個字符*表示0個或者多個+表示一個或者多個表示一個或者0

中括號代表的是一個範圍(但是隻是去裏面的一個值)比如“a.matches([abc])true

\s 空白字符:[ \t\n\x0B\f\r]    \S非空白字符:[ ^\t\n\x0B\f\r]  

\w單詞字符:[a-zA-Z_0-9]           \W非單詞字符:[^a-zA-Z_0-9]

\d 數字:[0-9]\D非數字:[^0-9]

\   正則表達式裏面反斜槓表示轉義,匹配兩個反斜槓的時候,就要用“\\\\

POSIX

\p{Lower}

小寫字母字符:[a-z]

\p{Upper}

大寫字母字符:[A-Z]

\p{ASCII}

所有 ASCII[\x00-\x7F]

\p{Alpha}

字母字符:[\p{Lower}\p{Upper}]

\p{Digit}

十進制數字:[0-9]

\p{Alnum}

字母數字字符:[\p{Alpha}\p{Digit}]

\p{Punct}

標點符號:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

\p{Graph}

可見字符:[\p{Alnum}\p{Punct}]

\p{Print}

可打印字符:[\p{Graph}\x20]

\p{Blank}

空格或製表符:[ \t]

\p{Cntrl}

控制字符:[\x00-\x1F\x7F]

\p{XDigit}

十六進制數字:[0-9a-fA-F]

\p{Space}

空白字符:[ \t\n\x0B\f\r]

\b 表示單詞的邊界 ^ []裏面表示非,在正則表達式開頭表示行的開頭

$表示的是行的結尾


Matcher.find()表示嘗試查找與該模式匹配的輸入序列的“下”一個子序列,如果不用Matcher.reset()的話,它就相當於指針指向上一個find執行之後的一個字符。

Matcher.lookingAt()嘗試將從區域開頭開始的輸入序列與該模式匹配

Matcher.start()Matcher.end()表示返回以前匹配的初始索引和返回最後匹配字符之後的偏移量


首先上一個簡單的正則表達式的例子:

publicclass SimpleReg {

publicstaticvoid main(String[] args) {

      System.out.println("bbc".matches("..."));

   }

}

/***output

true

**/

          通過例子來說明正則表達式的應用

/**

       * 直接用String裏面的matches()方法來匹配

       */

      System.out.println("abc".matches("[a-z]{3}"));

/**

       * 這是利用了java.util.regex.*;裏面的MatcherPattern

       * 這樣的效率會高,因爲預編譯了正則表達式

       */

      String input = "[a-z]{3}";

      Pattern p = Pattern.compile(input);//預編譯正則表達式

      Matcher m = p.matcher("abc");       //進行匹配

      System.out.println(m.matches());   //輸出匹配結果,注意Matches表示的整個式子

       

System.out.println("a".matches("."));

      System.out.println("aaaaa".matches("a*"));

      System.out.println("".matches("a*"));

      System.out.println("a".matches("a?"));

      System.out.println("".matches("a?"));

      System.out.println("a".matches("a+"));

      System.out.println("aaaa".matches("a+"));

      System.out.println("aaaaa".matches("aaaaa"));

              System.out.println("192.168.1.6".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));

      System.out.println("192".matches("[1-2][0-9][0-9]"));

       

System.out.println("a".matches("[abc]"));//abc裏面的一個元素

   System.out.println("a".matches("[^abc]"));   //abc元素

   System.out.println("B".matches("[abc]|[A-Z]"));  //並集

   System.out.println("D".matches("[FJD&&[A-Z]]"));//交集

   

System.out.println(" \n\t\r".matches("\\s{4}")); //四個空白字符的匹配

      System.out.println("s".matches("\\S"));           // \\S表示非空白字符

      System.out.println("a_8".matches("\\w+"));       // \\w表示字母字符([a-z_[0-9]])

      System.out.println("abc888^%#".matches("[a-z]{1,3}\\d+[#$%^]+"));

      System.out.println("".matches(""));

      System.out.println("\\".matches("\\\\"));

   

      System.out.println("a".matches("\\p{Lower}")); // "\p{Lower}"匹配一個字符是否是[a-z]

      System.out.println("ab".matches("\\p{Lower}"));//輸出爲false

      System.out.println("hello sir".matches("^h.*"));  //^表示行開頭

      System.out.println("hello sir".matches(".*r$")); //表示行結尾

       //其實代碼裏面的那個小點很重要

      System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$"));//帶有空格的空白行

       



      //find() start() end()lookingAt() group()方法的使用和作用

       

       String input="Hello java , you kown what i will love youforever.my java,you                     java";                

       Pattern p = Pattern.compile("java");

       Matcher m =p.matcher(input);


       while(m.find()){

          System.out.println(m.group()); //輸出找到的匹配的字串

       }

      System.out.println(m.matches());   //整個inputp相比,答案是false

      System.out.println(m.find());       //找到第一個匹配的字串  true

      System.out.println(m.start()+"-"+m.end());   //輸出的的開始和終結位置

      System.out.println(m.find());             //true

      System.out.println(m.start()+"-"+m.end());    

      System.out.println(m.find());               //true

      System.out.println(m.start()+"-"+m.end());

       //m.reset();         //重置,可以重新開頭找  reset(String str);可以重置已有的Matcher對象

      System.out.println(m.find());         //false因爲已經找到最後一個了


      System.out.println(m.lookingAt());      //找開頭匹配,均爲false

      System.out.println(m.lookingAt());

      System.out.println(m.lookingAt());

      System.out.println(m.lookingAt());


       

      //特殊構造之非捕獲

      String input = "1245aa4548578ba";

   //     Pattern p = Pattern.compile(".{3}(?=a)");  //a結尾的四個字符中的前三個字符

      Pattern p = Pattern.compile(".{3}(?!a)");  //不以a結尾的四個字符中的前三個字符

      Matcher m = p.matcher(input);

       while(m.find()){

          System.out.println(m.group());

       }


       

組的匹配比如(\\d\\d)\\1  表示後面一個組(\\1)要和第一個組相同,比如1212就是匹配

String input = "1212";

      Pattern p = Pattern.compile("(\\d\\d)\\1");

      Matcher m = p.matcher(input);

      System.out.println(m.matches());

     



       再比如:

                 //true

String input = "122";

      Pattern p = Pattern.compile("(\\d(\\d))\\2");

      Matcher m = p.matcher(input);

          System.out.println(m.matches());


       

這兩句話的作用相等。標記的作用!!!

       Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);

          Patternp = Pattern.compile("(?i)(java)");


       

   Greedy數量詞(吞最大的數) Reluctant 數量詞(吞最小的數) Possessive 數量詞(吞最大的數,但不吐出來)

       


       Greedy output0-10         (什麼也不加)先吞最大,再吐

       Pattern p = Pattern.compile(".{3,10}\\d");

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

       if(m.find())

          System.out.println(m.start()+"-"+m.end());

       else

          System.out.println("not Matcher!");


       

      Reluctant output0-6         (加?)先吞最少,再加吞

       Pattern p = Pattern.compile(".{3,10}?\\d");

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

       if(m.find())

          System.out.println(m.start()+"-"+m.end());

       else

          System.out.println("not Matcher!");


   Possessive  output:not Matcher!   (+)吞最多,不吐,效率快

       Pattern p = Pattern.compile(".{3,10}+\\d");

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

       if(m.find())

          System.out.println(m.start()+"-"+m.end());

       else

          System.out.println("not Matcher!");








































           


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章