正则表达式

mac2026-01-21  4

正则表达式

主要用于操作字符串虽然减少了代码量,阅读性差了

&与|或[abc]匹配abc中的一个,要么是a,要么是b,要么是c()组默认第1组()\1第一组出现1次()\1+第一组出现多次;从左往右看左括号((a)(b©)) 第一组(a)(b©) 第二组a 第三组b© 第四组c\\\

以下均需要两个\

.任意字符\d数字\s数字\w单词字符[a-zA-Z0-9_]

数量词

X?0次或1次X*任意次X+至少1次X{n}正好n次X{n,}至少n次X{n,m}至少n次,最多m次

边界匹配器

^行的开头$行的结尾(在后一个参数可以对前一个参数的组进行获取)\b单词边界\B非单词边界

常用操作

匹配 使用String类的matches方法切割 使用String类的split方法替换 使用String类的replaceAll(替换内容,正则表达式)方法,根据正则表达式替换获取 i. 将正则表达式封装为对象Pattern Pattern p = Pattern.compile(“正则表达式”); ii. 创建匹配器对象Matcher Matcher m = p.matcher(“需要操作的字符串”);       Matcher有三个方法       A. matches 匹配       B. lookingAt 从头匹配       C. find 查找匹配子串 iii. 查找m.find();//查找一个,返回布尔值 iv. 获取匹配的子序列m.group();//仅仅获取第一个 public class Test { public static void main(String[] args) { //匹配邮箱 boolean matches = "112Qe1@qq.cn".matches("[0-9a-zA-Z]{5,15}@{1}(qq|QQ|163|136)\\.{1}(com|cn|net)");//匹配参数(参数为正则表达式) System.out.println(matches); //切割字符串 String[] split = "我/爱@你,嫁 给 我.吧".split("[//@, \\.]{1,}"); for (String string : split) { System.out.println(string); } String str = "zhangsanRRRRR李lisiQQQQQQQwangwu"; String tel = "15630667516";//15630667516替换为156****7516 //将多个R和Q替换为相应一个 str = str.replaceAll("(.)\\1+", "$1"); System.out.println(str); tel = tel.replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$3"); System.out.println(tel); //获取三个字符的字符串 String str1 = "da jia hao ming tian bu fang jia"; System.out.println(str1); Pattern p = Pattern.compile("\\b[a-z]{3}\\b"); Matcher m = p.matcher(str1); while(m.find()) { System.out.println(m.group());//获取find()匹配到的子序列 System.out.println(m.start()+"-"+m.end());//获取找到子序列的开头结尾下标 } } }

运行结果

爬虫练习

/** * 网页爬虫:就是一个程序用于在互联网中获取符合指定规则的数据 * * 爬取邮箱地址 */ public class Test01 { public static void main(String[] args) throws IOException { List<String> mail = getMailWeb(); for (String string : mail) { System.out.println(string); } } //爬取本地文件中邮箱 public static List<String> getMail() throws IOException{ //1.读取源文件 BufferedReader br = new BufferedReader(new FileReader("本地文件地址")); //2.对读取的数据进行规则匹配。从中获取符合规则的数据 String regex = "[0-9a-zA-Z]{5,15}@{1}\\w{2,}\\.{1}(com|cn|net)"; List<String> list = new ArrayList<>(); Pattern p = Pattern.compile(regex); String line = null; while((line = br.readLine()) != null) { Matcher m = p.matcher(line); while(m.find()) list.add(m.group()); } //3.将符合规则的数据存到集合中 return list; } //爬取网络中邮箱 public static List<String> getMailWeb() throws IOException{ //1.读取源文件 //BufferedReader br = new BufferedReader(new FileReader("")); //URL url = new URL("网页地址"); URL url = new URL("https://zhidao.baidu.com/question/1772307510687057620.html"); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); //2.对读取的数据进行规则匹配。从中获取符合规则的数据 String regex = "[0-9a-zA-Z]{5,15}@{1}\\w{2,}\\.{1}(com|cn|net)"; List<String> list = new ArrayList<>(); Pattern p = Pattern.compile(regex); String line = null; while((line = br.readLine()) != null) { Matcher m = p.matcher(line); while(m.find()) list.add(m.group()); } //3.将符合规则的数据存到集合中 return list; } }

运行结果

爬取小说,写入文件,下一章翻页

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PaChong { public static String next; public static void main(String[] args) throws IOException { // TODO 自动生成的方法存根 String str1 = "http://www.biqu6.com"; System.out.println(ToolString(getMailWeb(str1+"/1_1821/1157521.html"))); System.out.println(); if(saveBook(ToolString(getMailWeb(str1+"/1_1821/1157521.html")))) System.out.println("SiveOk"); System.out.println(); System.out.println("下一章地址:"+str1+getNex()); System.out.println(); System.out.println(ToolString(getMailWeb(str1+getNex()))); System.out.println(); System.out.println("下一章地址:"+str1+getNex()); System.out.println(); System.out.println(ToolString(getMailWeb(str1+getNex()))); System.out.println(); System.out.println("下一章地址:"+str1+getNex()); } public static String getMailWeb(String inter) throws IOException{ //1.读取源文件 //BufferedReader br = new BufferedReader(new FileReader("")); //URL url = new URL("网页地址"); //String inter = "http://www.biqu6.com/1_1821/1157521.html"; URL url = new URL(inter); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); //2.对读取的数据进行规则匹配。从中获取符合规则的数据 String regex1 = "(div id=\"content\")(.)*(/div)"; String regex2 = "(<a href=\")(.){10,30}(\">下一章</a>)"; StringBuffer book = new StringBuffer(); Pattern p1 = Pattern.compile(regex1); Pattern p2 = Pattern.compile(regex2); String line = null; while((line = br.readLine()) != null) { Matcher m1 = p1.matcher(line); Matcher m2 = p2.matcher(line); while(m1.find()) { book.append(line); } while(m2.find()) { getNexString(m2.group()); } } //3.将符合规则的数据存到集合中 return book.toString(); } //书籍排版 public static String ToolString(String str) { str = str.replaceAll("(&nbsp;)", " "); str = str.replaceAll("(//)(.)*(//)", ""); str = str.replaceAll("(<br/>){1,2}", "\r\n"); str = str.replaceAll("(<)(.){4,20}(>)", ""); str = str.replaceAll("(\\*){4,20}(.)*", ""); return str; } //下一章<a href="/1_1821/1157522.html">下一章</a> public static String getNex() { String str = next.replaceAll("(<a href=\")(/{1}\\w{3,}/{1}\\w{3,}\\.(html))(\">下一章</a>)", "$2"); return str; } public static void getNexString(String str) { next =str; } public static boolean saveBook(String str) { boolean b = false; try ( BufferedWriter bw = new BufferedWriter(new FileWriter("D:/1.doc")); ){ bw.write(str); b = true; } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return b; } }

运行结果

最新回复(0)