一:介绍
1.简单的匹配
String中有一个matches方法。
1 public class Test131 { 2 public static void main(String[] args) { 3 regexDemo(); 4 } 5 public static void regexDemo(){ 6 String qqNum="0298278"; 7 //其中4,14是一个个数范围 8 String regex="[1-9][0-9]{4,14}"; 9 boolean flag=qqNum.matches(regex); 10 if(flag){ 11 System.out.println("ok"); 12 }else{ 13 System.out.println("error"); 14 } 15 } 16 }
2.主要方法
匹配 matches(regex)
切割 split(regex)
替换 replaceAll(regex,replacement)
获取 特殊一点
二:匹配matches(regex)
1.主要的regex
2.字符类表达式
3.预定义字符类
4.数量词
5.程序
程序结果是true。
1 public class Test132 { 2 public static void main(String[] args) { 3 demo(); 4 } 5 public static void demo() 6 { 7 String str = "b23a23456789"; 8 String reg = "[a-zA-Z]\d*[a-z]\d*"; 9 boolean b= str.matches(reg); 10 System.out.println(b); 11 } 12 }
三:切割split(regex)
1.先写一个简单的程序
使用,进行切割。
1 public class Test133 { 2 public static void main(String[] args) { 3 splitDemo(); 4 } 5 public static void splitDemo(){ 6 String str="zhangsan,lisi,wangwu"; 7 String regex=","; 8 String[] name=str.split(regex); 9 for(String n:name){ 10 System.out.println(n); 11 } 12 } 13 }
2.特殊的切割
对 . 的切割
对\\的切割
对叠词的切割
1 public class Test134 { 2 public static void main(String[] args) { 3 //pointDemo(); 4 //virguleDemo(); 5 reduplicatedDemo(); 6 } 7 public static void pointDemo(){ 8 String str="zhangsan.lisi.wangwu"; 9 String regex="\."; 10 String[] strs=str.split(regex); 11 for(String n :strs){ 12 System.out.println(n); 13 } 14 } 15 public static void virguleDemo(){ 16 String str="c:\abd\yy.txt"; 17 String regex="\\"; 18 String[] strs=str.split(regex); 19 for(String n :strs){ 20 System.out.println(n); 21 } 22 } 23 public static void reduplicatedDemo(){ 24 String str="mhuujoptttttqb"; 25 String regex="(.)\1+"; 26 String[] strs=str.split(regex); 27 for(String n :strs){ 28 System.out.println(n); 29 } 30 } 31 }
四:替换replaceAll(regex,replacement)
1.API
2.程序
将数字替换成#
将叠词替换成&
将叠词替换成单个单词
1 public class Test135 { 2 public static void main(String[] args) { 3 //numDemo(); 4 //andDemo(); 5 sigleDemo(); 6 } 7 /** 8 * 将字符串替换成# 9 */ 10 public static void numDemo(){ 11 String str="ghsgdgdgs2324oop996uuuu"; 12 String regex="\d{1,}"; 13 String result=str.replaceAll(regex, "#"); 14 System.out.println("result="+result); 15 } 16 /** 17 * 将叠词变成& 18 */ 19 public static void andDemo(){ 20 String str="ghsssyjguuuu"; 21 String regex="(.)\1+"; 22 String result=str.replaceAll(regex, "&"); 23 System.out.println("result="+result); 24 } 25 /** 26 * 将叠词变成单个单词 27 */ 28 public static void sigleDemo(){ 29 String str="ghsssyjguuuu"; 30 String regex="(.)\1+"; 31 String result=str.replaceAll(regex, "$1"); 32 System.out.println("result="+result); 33 } 34 }
五:获取
1.操作步骤
1,将正则表达式封装成对象。
2,让正则对象和要操作的字符串相关联。
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的子串进行操作,比如取出。
2.程序
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 public class Test136 { 4 public static void main(String[] args) { 5 groupDemo(); 6 } 7 public static void groupDemo(){ 8 String str="yub jjjjj hhhh uuu oo sds"; 9 //其中的意思的单词边界匹配符 10 String regex="\b[a-z]{3}\b"; 11 12 //将规则封装成对象 13 Pattern p=Pattern.compile(regex); 14 //关联,获取匹配器对象 15 Matcher m=p.matcher(str); 16 17 while(m.find()){ 18 String str1=m.group(); 19 System.out.println("str1="+str1); 20 } 21 } 22 }
3.运行结果
六:网页爬虫
1.介绍
需要网络连接
2.程序
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.net.URL; 5 import java.net.URLConnection; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 public class Test137 { 9 public static void main(String[] args) throws Exception{ 10 urlDemo(); 11 } 12 public static void urlDemo() throws Exception{ 13 //将main.html文件放在E盘下 14 URL url = new URL("file:///E:/mail.html"); 15 URLConnection conn = url.openConnection(); 16 17 //获取网络上的数据 18 BufferedReader bufIn = new BufferedReader( 19 new InputStreamReader(conn.getInputStream())); 20 String line = null; 21 22 String mailreg = "\w+@\w+(\.\w+)+"; 23 Pattern p = Pattern.compile(mailreg); 24 while((line=bufIn.readLine())!=null) 25 { 26 Matcher m = p.matcher(line); 27 while(m.find()) 28 { 29 System.out.println(m.group()); 30 } 31 } 32 } 33 }
3.运行结果