又认真看了一次正则表达式的相关的内容,发现,好记性不如烂笔头,古人诚不欺我也,所以写下这篇博文,一是为了跟大家分享一下,第二,方便那天我忘记正则表达式的内容了,就回头查看一下...如果有什么不恰当的地方,望浏览者能够大方指出,便于相互学习。



\\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!");