java 正则 ava - 正则表达式 - Pattern - Matcher 正则表达式概念 正则表达式的基本语法 扩展内容 String中正则表达式的使用 Pattern概述 Pattern标志常量 Pattern常用方法 Matcher概述 Matcher常用方法 网页中揪出Email

java 正则
ava - 正则表达式 - Pattern - Matcher
正则表达式概念
正则表达式的基本语法
扩展内容
String中正则表达式的使用
Pattern概述
Pattern标志常量
Pattern常用方法
Matcher概述
Matcher常用方法
网页中揪出Email

java 正则
ava - 正则表达式 - Pattern - Matcher
正则表达式概念
正则表达式的基本语法
扩展内容
String中正则表达式的使用
Pattern概述
Pattern标志常量
Pattern常用方法
Matcher概述
Matcher常用方法
网页中揪出Email 分类:
 
 

目录(?)[+]

 

正则表达式概念

[java] view plain copy
 
  1. 所谓正则表达式就是处理字符串的特殊字符串  
  2. 用途  
  3.     字符串匹配(字符匹配)  
  4.     字符串查找(是建立在匹配之上的查找)  
  5.     字符串替换(是建立在查找的结果之后的替换)  
  6. 例如  
  7.     IP地址是否正确  
  8.     从网页中揪出Email地址  
  9.     从网页揪出链接  
  10. 类  
  11.     java.lang.String  
  12.     java.util.regex.Pattern  
  13.     java.util.regex.Matcher  
  14. 要点        
  15.     //一个反斜线  
  16.     String str1 = "\";  
  17.     //正则表达式的一个反斜线使用两个反斜线,在java中表示就再需要两个反斜线转义  
  18.     System.out.println(str1.matches("\\"));  
  19.     //POSIX Style,这种写法不建议使用,如果要使用查API  
  20.     System.out.println("a".matches("\p{Lower}"));  
  21.     //空白行  
  22.     "  ".matches("^[\s&&[^ ]]*\n$");  
  23.     //邮箱  
  24.     String regex1 = "[\w[.-]]+";  
  25.     String regex2 = "@";  
  26.     String regex3 = "[\w[.-]]+";  
  27.     String regex4 = "\.";  
  28.     String regex5 = "[\w]+";  
  29.     String regex = regex1+regex2+regex3+regex4+regex5;  
  30.     System.out.println("156112846@qq.com".matches(regex));  

正则表达式的基本语法

[java] view plain copy
 
  1. 普通字符(字母,数字,汉字,下划线)        
  2.     一个普通字符在表达式中只匹配与之相同的一个字符  
  3.     表达式k在字符串sky进行匹配时,将匹配成功  
  4. , , ,f  
  5.     表示回车符,换行符,制表符,换页符  
  6. .     
  7.     任意一个字符  
  8. X?    
  9.     表示X可以出现0次或者1次  
  10. X+    
  11.     表示X可以出现1次或者多次  
  12. X*  
  13.     表示X可以出现任意次  
  14. X{n}  
  15.     表示X可以出现n次  
  16. X{m,n}  
  17.     表示X可以最少出现m次,最多出现n次  
  18. X{n,}  
  19.     表示X最少出现n次  
  20. [ ]  
  21.     匹配中括号中任意一个字符  
  22. [^ ]  
  23.     匹配中括号中字符之外的任意一个字符  
  24. d  
  25.     表示0~9之间的任意一个数字字符,即[0-9]  
  26. D  
  27.     表示0~9之外的任意数字字符,即[^0-9]  
  28. , , ,f  
  29.     表示回车符,换行符,制表符,换页符  
  30. s  
  31.     表示空格,制表符,换页符等空白字符的任意一个  
  32. S  
  33.     表示空白字符以外的任意一个字符,即[^s]  
  34. w  
  35.     表示字母,数字,下划线中的任意一个字符,即[a-zA-Z_0-9]  
  36. W  
  37.     表示字母,数字,下划线以外的任意一个字符,即[^a-zA-Z_0-9]  
  38. ^  
  39.     该符号不匹配任何字符,字符串开始的位置,即^h必须以h开头  
  40. $  
  41.     该符号不匹配任何字符,字符串结束的位置,即r$必须以r结尾  
  42.   
  43.     该符号不匹配任何字符,表示单词的边界  
  44. B  
  45.     该符号不匹配任何字符,表示非单词的边界,即[^]  
  46. |  
  47.     用来连接两个表达式,表示或的关系  
  48.     X|Y 表示X或者Y中的任意字符  
  49. ()  
  50.     作为一个单元,一个分组  
  51. (n表示一个数字)  
  52.     有分组的情况下,表示对分组的引用  
  53.     1表示对分组1的引用  
  54.   
  55.     转义字符,当一个符号自身有意义而又要表示这个字符的时候,就需要转义  
  56.     ^表示^,$表示$  
  57. ?  
  58.     如果在?,+,*,{n},{m,n},{n,}后面,表示次数按非贪婪模式进行匹配,  
  59.         即按照匹配模式进行最小限度的匹配  

扩展内容

 

贪婪 VS 非贪婪 VS 独占

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws Exception {  
  8.         String str = "aaaa5bbbb6";  
  9.   
  10.         /* 
  11.          * 贪婪的 
  12.          * 一次吃进最多的10个字符,不匹配,再吐出来一个,匹配了,结束了 
  13.          */  
  14.         String regex1 = ".{3,10}[0-9]";  
  15.         Matcher m1 = Pattern.compile(regex1).matcher(str);  
  16.         if (m1.find())  
  17.             System.out.println(m1.start() + "-" + m1.end());  
  18.         else  
  19.             System.out.println("not match!");  
  20.   
  21.         /* 
  22.          * 非贪婪的 
  23.          * 一次吃进最少的3个字符,然后看后面哪个是不是数字, 
  24.          * 再吞一个,然后再看后面哪个是不是数字,匹配了,结束了 
  25.          */  
  26.         regex1 = ".{3,10}?[0-9]";  
  27.         m1 = Pattern.compile(regex1).matcher(str);  
  28.         if (m1.find())  
  29.             System.out.println(m1.start() + "-" + m1.end());  
  30.         else  
  31.             System.out.println("not match!");  
  32.   
  33.         /* 
  34.          * 独占的 
  35.          * 一次吃进最多的10个字符,不匹配,不吐出来,不匹配了,结束了 
  36.          */  
  37.         regex1 = ".{3,10}+[0-9]";  
  38.         m1 = Pattern.compile(regex1).matcher(str);  
  39.         if (m1.find())  
  40.             System.out.println(m1.start() + "-" + m1.end());  
  41.         else  
  42.             System.out.println("not match!");  
  43.     }  
  44. }  
  45. /* 
  46. 打印结果: 
  47.     0-10 
  48.     0-5 
  49.     not match! 
  50. */  

非捕获组

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws Exception {  
  8.         System.out.println("--打印以a结尾的子串---");  
  9.         String str = "444a66b";  
  10.         String regex = ".{3}a";  
  11.         Matcher m = Pattern.compile(regex).matcher(str);  
  12.         while (m.find())  
  13.             System.out.println(m.group());  
  14.   
  15.         System.out.println("--打印以a结尾的子串,但不捕获a--");  
  16.         // 非捕获组,这块的意思是不捕获a,算在这三个字符之中的  
  17.         regex = ".{3}(?=a)";  
  18.         m = Pattern.compile(regex).matcher(str);  
  19.         while (m.find())  
  20.             System.out.println(m.group());  
  21.   
  22.         System.out.println("--这个例子和上面例子相似,但结果出乎意料--");  
  23.         // 非捕获组,不算在这三个字符之中的  
  24.         regex = "(?=a).{3}";  
  25.         m = Pattern.compile(regex).matcher(str);  
  26.         while (m.find())  
  27.             System.out.println(m.group());  
  28.   
  29.         System.out.println("--打印前面不能是a的子串");  
  30.         regex = "(?!a).{3}";  
  31.         m = Pattern.compile(regex).matcher(str);  
  32.         while (m.find())  
  33.             System.out.println(m.group());  
  34.   
  35.         System.out.println("--这个例子和上面例子相似,结果需要注意--");  
  36.         // 这个是先找444,后面是a不匹配再找44a,后面不是a,匹配  
  37.         regex = ".{3}(?!a)";  
  38.         m = Pattern.compile(regex).matcher(str);  
  39.         while (m.find())  
  40.             System.out.println(m.group());  
  41.   
  42.         regex = ".{3}(?<!a)";  
  43.     }  
  44. }  
  45. /* 
  46. 打印结果: 
  47.     --打印以a结尾的子串--- 
  48.     444a 
  49.     --打印以a结尾的子串,但不捕获a-- 
  50.     444 
  51.     --这个例子和上面例子相似,但结果出乎意料-- 
  52.     a66 
  53.     --打印前面不能是a的子串 
  54.     444 
  55.     66b 
  56.     --这个例子和上面例子相似,结果需要注意-- 
  57.     44a 
  58.     66b 
  59. */  

向前引用

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws Exception {  
  8.         // 第一组找到12,1的意思是后面的必须和第一组一样  
  9.         Matcher m1 = Pattern.compile("(\d\d)\1").matcher("1212");  
  10.         System.out.println(m1.matches());  
  11.           
  12.         //1212不匹配,121不匹配,122匹配  
  13.         Matcher m2 = Pattern.compile("(\d(\d))\2").matcher("1212");  
  14.         System.out.println(m2.matches());  
  15.     }  
  16. }  
  17. /* 
  18. 打印结果: 
  19.     true 
  20.     false 
  21. */  

flags简写

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Pattern;  
  4.   
  5. public class Test {  
  6.     public static void main(String[] args) throws Exception {  
  7.         Pattern.compile("java", Pattern.CASE_INSENSITIVE);  
  8.         //上下代码是等价的  
  9.         System.out.println("JAVA".matches("(?i)(java)"));  
  10.         System.out.println("java".matches("(?i)(java)"));  
  11.     }  
  12. }  
  13. /* 
  14. 打印结果: 
  15.     true 
  16.     true 
  17. */  

String中正则表达式的使用

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) throws Exception {  
  5.         String str = "abc:0000:defg";  
  6.         System.out.println(str.replace("0000","8765"));  
  7.         // 替换所有匹配正则表达式的  
  8.         System.out.println(str.replaceAll("\d", "0"));  
  9.         // 只替换第一个匹配正则表达式的  
  10.         System.out.println(str.replaceFirst("\d", "1"));  
  11.         // str是否匹配给定正则表达式  
  12.         System.out.println(str.matches(".{13}"));  
  13.         //拆分  
  14.         for(String s : str.split(":")){  
  15.             System.out.print(s+" ");  
  16.         }  
  17.         System.out.println();  
  18.         //拆分几段  
  19.         for(String s : str.split(":",2)){  
  20.             System.out.print(s+" ");  
  21.         }   
  22.     }  
  23. }  
  24. /* 
  25. 打印结果: 
  26.     abc:8765:defg 
  27.     abc:0000:defg 
  28.     abc:1000:defg 
  29.     true 
  30.     abc 0000    defg     
  31.     abc 0000:defg 
  32. */  

Pattern概述

[java] view plain copy
 
  1. java.util.regex.Pattern   
  2.     其对象表示通过编译的正则式,利用该类对象可以与任意字符串进行模式匹配  
  3. 构造器  
  4.     Pattern类的构造器是private  
  5. 声明  
  6.     public final class Pattern extends Object implements Serializable  
  7. 创建Pattern的静态工厂  
  8.     public static Pattern compile(String regex)  
  9.         将指定正则式编译成Pattern对象返回  
  10.     public static Pattern compile(String regex,int flags)  
  11.         将指定正则式按照指定标志编译成Pattern对象返回  

Pattern标志常量

[java] view plain copy
 
  1. public static final int CASE_INSENSITIVE  
  2.     将启动对ASCII字符不区分大小写匹配  
  3. public static final int UNICODE_CASE  
  4.     将启动Unicode字符不区分大小写匹配  
  5. public static final int DOTALL  
  6.     将启动dotall模式,该模式下,"."将表示任意字符,包括回车符  

Pattern常用方法

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws Exception {  
  8.         // 编译好的模式用起来更快一些  
  9.         // 将给定的正则表达式编译到模式中  
  10.         Pattern p = Pattern.compile("[a-z]{3}");  
  11.         // 将给定的正则表达式编译到具有给定标志的模式中  
  12.         p = Pattern.compile("[a-z]{3}", Pattern.CASE_INSENSITIVE);  
  13.         // 返回此模式的匹配标志  
  14.         System.out.println(p.flags());  
  15.         // 匹配某个字符串之后的结果,这个有可能有很多个  
  16.         // 该方法返回表示将要被匹配字符序列的匹配器对象  
  17.         Matcher m = p.matcher("fgh");  
  18.         System.out.println(m.matches());  
  19.         // 编译给定正则表达式并尝试将给定输入与其匹配  
  20.         System.out.println(Pattern.matches("[a-z]{3}", "fgh"));  
  21.         // 返回在其中编译过此模式的正则表达式  
  22.         System.out.println(p.pattern());  
  23.         // 该方法返回指定字符串的模式文本表示  
  24.         System.out.println(Pattern.quote("abc"));  
  25.         // 匹配拆分  
  26.         String[] str1 = Pattern.compile(",").split("boo,and,foo");  
  27.         for (String s : str1) {  
  28.             System.out.print(s + " ");  
  29.         }  
  30.         System.out.println();  
  31.         // 匹配拆分,拆分几段  
  32.         String[] str2 = Pattern.compile(":").split("boo:and:foo", 2);  
  33.         for (String s : str2) {  
  34.             System.out.print(s + " ");  
  35.         }  
  36.     }  
  37. }  
  38. /* 
  39. 打印结果: 
  40.     2 
  41.     true 
  42.     true 
  43.     [a-z]{3} 
  44.     QabcE 
  45.     boo and foo  
  46.     boo and:foo  
  47. */   

Matcher概述

[java] view plain copy
 
  1. java.util.regex.Matcher 匹配器   
  2. 声明    
  3.     public final class Matcher extends Object implements MatchResult   

Matcher常用方法

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws Exception {  
  8.         Matcher m1 = Pattern.compile("\d{3,5}").matcher("123-45682-152-00");  
  9.         // 返回由此匹配器解释的模式  
  10.         System.out.println(m1.pattern());  
  11.         // 只有整个字符序列完全匹配成功才返回true  
  12.         System.out.println(m1.matches());  
  13.         // 把原来吃点的吐出来恢复到从前的状态  
  14.         m1.reset();  
  15.         // 找一个和模式匹配的子串,找到之后正则表达式引擎会把第一个子串去掉  
  16.         System.out.println(m1.find());  
  17.         // 起始位置和结束位置,但必须能找到,否则报错  
  18.         System.out.println(m1.start() + "-" + m1.end());  
  19.         // 每次都从开始位置找  
  20.         System.out.println(m1.lookingAt());  
  21.   
  22.         System.out.println("------------------------------");  
  23.         CharSequence cs = "java JAVA javaEE hiJAVA abcdefg";  
  24.         int i = Pattern.CASE_INSENSITIVE;  
  25.         Matcher m2 = Pattern.compile("java", i).matcher(cs);  
  26.         while (m2.find()) {  
  27.             // 分组,输出匹配到的子串  
  28.             System.out.print(m2.group() + " ");  
  29.         }  
  30.   
  31.         System.out.println();  
  32.         System.out.println("------------------------------");  
  33.         // 把java全部替换成JAVA  
  34.         System.out.println(m2.replaceAll("JAVA"));  
  35.   
  36.         System.out.println("------------------------------");  
  37.         // 把单数java转换为小写,双数java转换为大写  
  38.         m2.reset();  
  39.         StringBuffer sb = new StringBuffer();  
  40.         int count = 0;// 奇数偶数的常量  
  41.         while (m2.find()) {  
  42.             count++;  
  43.             if (count % 2 == 0) {  
  44.                 // 替换  
  45.                 m2.appendReplacement(sb, "JAVA");  
  46.             } else {  
  47.                 m2.appendReplacement(sb, "java");  
  48.             }  
  49.         }  
  50.         // 添加尾巴,否则abcdefg不显示  
  51.         m2.appendTail(sb);  
  52.         System.out.println(sb);  
  53.   
  54.         System.out.println("------------------------------");  
  55.         // 取符合正则表达式  
  56.         Pattern p = Pattern.compile("\d{3,5}[a-z]{2}");  
  57.         Matcher m3 = p.matcher("123aa-33345bb-234cc-00");  
  58.         while (m3.find()) {  
  59.             System.out.print(m3.group() + " ");  
  60.         }  
  61.         System.out.println();  
  62.         // 使用分组取数字,有几个(就分了几组  
  63.         // 怎么知道是第几组,规律是(是第几个就是第几组  
  64.         p = Pattern.compile("(\d{3,5})([a-z]{2})");  
  65.         m3 = p.matcher("123aa-33345bb-234cc-00");  
  66.         while (m3.find()) {  
  67.             // 只要第一组数字  
  68.             System.out.print(m3.group(1) + " ");  
  69.         }  
  70.         m3.reset();  
  71.         System.out.println();  
  72.         while (m3.find()) {  
  73.             // 只要第一组字母  
  74.             System.out.print(m3.group(2) + " ");  
  75.         }  
  76.     }  
  77. }  
  78. /* 
  79. 打印结果: 
  80.     d{3,5} 
  81.     false 
  82.     true 
  83.     0-3 
  84.     true 
  85.     ------------------------------ 
  86.     java    JAVA    java    JAVA     
  87.     ------------------------------ 
  88.     JAVA JAVA JAVAEE hiJAVA abcdefg 
  89.     ------------------------------ 
  90.     java JAVA javaEE hiJAVA abcdefg 
  91.     ------------------------------ 
  92.     123aa   33345bb 234cc    
  93.     123 33345   234  
  94.     aa  bb  cc 
  95. */  

网页中揪出Email

[java] view plain copy
 
  1. package com.itlwc;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.util.regex.Matcher;  
  6. import java.util.regex.Pattern;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) throws Exception {  
  10.         FileReader fr = new FileReader("D:\email.html");  
  11.         BufferedReader br = new BufferedReader(fr);  
  12.         String line = "";  
  13.         while ((line = br.readLine()) != null) {  
  14.             parse(line);  
  15.         }  
  16.     }  
  17.   
  18.     private static void parse(String line) {  
  19.         Pattern p = Pattern.compile("[\w[.-]]+@[\w[.-]]+\.[\w]+");  
  20.         Matcher m = p.matcher(line);  
  21.         while (m.find()) {  
  22.             System.out.println(m.group());  
  23.         }  
  24.     }  
  25. }