Java_正则表达式

来源:互联网 发布:选购冰箱知乎 编辑:程序博客网 时间:2024/06/05 14:36
本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/8957461

1.基本规则

    [abc] a、b、c中的一个
     [^abc] 除了abc
     [a-z&&d-f&&e-g] 在三个范围之内
     . 任意字符
     \d 数字0-9 [0-9]
     \s 空白字符 [\t\n\x0B\f\r] 
     \w 单词字符 [a-zA-Z_0-9] 
 
     Greedy 数量词 
        X?  X出现一次或者零次
       X*  X零次或多次 
       X+  X一次或多次 
        X{n} X恰好 n 次 
       X{n,} X至少 n 次 
       X{n,m} X至少 n 次不超过 m 次 


    ^h  表示以h开头
    h$  表示以h结尾

System.out.println("a".matches("\\w{1,2}"));//以首字母h开通System.out.println("hello".matches("^h\\w+"));//将后四位置为****System.out.println("12344321".replaceAll("\\d{4}$", "****"));//固定电话规则System.out.println("010-88880909-1212".matches("\\d{3,4}-\\d{7,8}(-\\d{1,7})?"));//邮箱规则System.out.println("zfs_09@163.com".matches("[\\w_-]+@[\\w_-]+(\\.(\\w+)){1,2}"));//ip规则System.out.println("192.168.1.249".matches("(((1[0-9])?[0-9]||[1-9][0-9]||25[0-5]||2[0-4][0-9])\\.){3}((1[0-9])?[0-9]||[1-9][0-9]||25[0-5]||2[0-4][0-9])"));

 

2.Pattern 和 Matcher 的重要使用

    1).使用步骤

String str = "412525198907015719, 520121199012031232,123909198703120978";Pattern p = Pattern.compile("(\\d{6})(\\d{8})(\\d{4})");//编译Matcher m = p.matcher(str);//将编译规则与字符串匹配m.matches();//判断是否匹配成功

    2).Matcher 的重要方法

       A).boolean matches(); 返回是否完全匹配,只要判断出不符合规则,就停止往后读取字符,隐含的指针停留在该位置!
       B).reset();将类部隐含的指针置于字符串的起始位置!
       c).int start(); 返回以前匹配的初始索引位置
       D).int end();返回最后匹配字符之后的偏移量。 
       E).boolean find();子字符串中寻找,只要找到就返回true,每次指针自动向后移动
       F).   String group();返回由以前匹配操作所匹配的输入子序列。
          String group(int);返回由以前匹配操作所匹配的输入子序列的组。
           ((\\d{1})(\\d{2}))(\\d{3})括号由左到右
           group(1)-->((\\d{1})(\\d{2}))
           group(2)-->(\\d{1})  //将index较小的,其中不是元组,而里面还包含组,将组分解完全
           group(3)-->(\\d{2}) group(4)-->(\\d{3})

   3).贪婪模式与非贪婪模式

          Pattern p2 = Pattern.compile("<td>.*</td>"); //贪婪模式

          Pattern p3 = Pattern.compile("<td>(.*?)</td>"); //非贪婪模式

String str2 = "<table><td>11啊11</td><td>22啊22</td><td>33啊33</td></table>";Pattern p2 = Pattern.compile("<td>.*</td>");//贪婪模式Matcher m2 = p2.matcher(str2);while(m2.find()){System.out.println(m2.group());//<td>11啊11</td><td>22啊22</td><td>33啊33</td>}Pattern p3 = Pattern.compile("<td>(.*?)</td>");//非贪婪模式Matcher m3 = p3.matcher(str2);while(m3.find()){System.out.println(m3.group(1));//11啊11//22啊22//33啊33}
package cn.itcast.StaticImport;import java.util.regex.Matcher;import java.util.regex.Pattern;/* * [abc] a、b、c中的一个 * [^abc] 除了abc * [a-z&&d-f&&e-g] 在三个范围之内 * . 任意字符 * \d 数字0-9 [0-9] * \s 空白字符 [\t\n\x0B\f\r]  * \w 单词字符 [a-zA-Z_0-9]  *  * Greedy 数量词  * X?  X出现一次或者零次 * X*  X零次或多次  * X+  X一次或多次  * X{n} X恰好 n 次  * X{n,} X至少 n 次  * X{n,m} X至少 n 次不超过 m 次  * ^h  表示以h开头 * h$  表示以h结尾 *  * Strawberry2013-5-21 */public class Test {public static void main(String[] args){System.out.println("12344321".replaceAll("\\d{4}$", "****"));System.out.println("a".matches("\\w{1,2}"));System.out.println("hello".matches("^h\\w+"));//固定电话规则System.out.println("010-88880909-1212".matches("\\d{3,4}-\\d{7,8}(-\\d{1,7})?"));//邮箱规则System.out.println("zfs_09@163.com".matches("[\\w_-]+@[\\w_-]+(\\.(\\w+)){1,2}"));//ip规则System.out.println("192.168.1.249".matches("(((1[0-9])?[0-9]||[1-9][0-9]||25[0-5]||2[0-4][0-9])\\.){3}((1[0-9])?[0-9]||[1-9][0-9]||25[0-5]||2[0-4][0-9])"));String str = "41252519890701571, 520121199012031232,123909198703120978";Pattern p = Pattern.compile("(\\d{6})(\\d{8})(\\d{4})");Matcher m = p.matcher(str);System.out.println(m.matches());while(m.find()){System.out.println("出生地:"+m.group(1)+"出生日期"+m.group(2));}String str1 = "123456789";Pattern p1 = Pattern.compile("((\\d{1})(\\d{2}))(\\d{3})");Matcher m1 = p1.matcher(str1);while(m1.find()){System.out.println(m1.group());//((\\d{1})(\\d{2}))(\\d{3}) 123456System.out.println(m1.group(1));//(\\d{1})(\\d{2})          123System.out.println(m1.group(2));//(\\d{1})                  1System.out.println(m1.group(3));//(\\d{2})                  23System.out.println(m1.group(4));//(\\d{3})                  456}String str2 = "<table><td>11啊11</td><td>22啊22</td><td>33啊33</td></table>";Pattern p2 = Pattern.compile("<td>.*</td>");//贪婪模式Matcher m2 = p2.matcher(str2);while(m2.find()){System.out.println(m2.group());//<td>11啊11</td><td>22啊22</td><td>33啊33</td>}Pattern p3 = Pattern.compile("<td>(.*?)</td>");//非贪婪模式Matcher m3 = p3.matcher(str2);while(m3.find()){System.out.println(m3.group(1));//11啊11//22啊22//33啊33}}}


 

原创粉丝点击