String的一些方法使用的就是封装过的正则。import java.util.regex.*; import java.io.*;
class RegexDemo { public static void main(String[] args) throws Exception { /* BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); while(str != "\r\n" && str != " ") { System.out.println(str); //checkQQ(str); checkTel(str); str = br.readLine(); } System.out.println("over"); */ //splitDemo("c:\\abc\\a.txt", ":?\\\\+"); //splitDemo("saddfaeefat", "(.)\\1");//按照叠词完成切割.为了可以让规则的结果被重用。 //可以将规则封装成一个组。用()完成。组的出现都有编号。 //从1开始。想要使用已有的组可以通过\n(n就是组的编号)的形式来获取。 String str = "dfs333af3234255ewae34234234242wfgd3323gwe"; //replaceAllDemo(str, "\\d{5,}", "#"); String str1 = "daqqsfaaaewfwzzzzzef";//将叠词替换成$。将重复的字母替换成单个字母zzzz->z replaceAllDemo(str1, "(.)\\1+", "$1"); } public static void replaceAllDemo(String str, String regex, String newStr) { str = str.replaceAll(regex, newStr); System.out.println(str); } public static void splitDemo(String str, String reg) { String[] arr = str.split(reg); System.out.println(arr.length); for(String s : arr) { System.out.println(s); } } /* 匹配 手机号段只有 13*** 15*** 18*** */ public static void checkTel(String tel) { //String rege = "1[358]\\d{2}"; String rege = "(13|15|18)\\d{2}"; boolean flag = tel.matches(rege); System.out.println(tel + " is tel? " + flag); } /** 对qq号码进行校验 要求:5~15 0不能开头,只能是数字 */ public static void checkQQ_1(String qq) { Pattern qqPattern = Pattern.compile("^[1-9][0-9]{4,14}$"); boolean isQQ = qqPattern.matcher(qq).matches(); System.out.println(qq + " is qq? " + isQQ); } public static void checkQQ(String qq) { String regex = "[1-9][0-9]{4,14}"; boolean isQ = qq.matches(regex); System.out.println(qq + " is qq? " + isQ); } }
import java.util.regex.*; class RegexDemo2 { public static void main(String[] args) { getDemo(); } public static void getDemo() { String str = "ming tian jiu yao fang jia le, da jia. "; String reg = "\\b[a-z]{3}\\b";// \b单词边界 //将规则封装成对象 Pattern p = Pattern.compile(reg); //让正则对象和要作用的字符串相关联,获取匹配器对象 Matcher m = p.matcher(str); //boolean b = m.find();//将规则作用到字符串上,并进行符合规则的子串查找。 //System.out.println(b); //System.out.println(m.group());//用于获取匹配后的结果 while(m.find()) { System.out.println(m.group() + " " + m.start() + "....." + m.end()); } } }
三、爬虫小窥import java.util.*; class RegexTest { public static void main(String[] args) { checkEmail(); } /* 需求:对邮件地址进行校验 */ public static void checkEmail() { String email = "dsafsafdsa@live.com"; email = "1@1.1"; String regex = "\\w+@\\w+(\\.[a-zA-Z]+)+"; //较为精确的匹配 regex = "\\w+@\\w+(\\.\\w+)+"; //相对不太精确的匹配 System.out.println(email + " is email format? " + email.matches(regex)); } /* 需求: 将下列字符串转成 我要学编程 到底用四种功能的哪一个?或者哪几个? 思路方式: 1、如果只想知道该字符串是否对错,使用匹配。 2、想要将已有的字符串变成另一个字符串,替换。 3、想要按照自定的方式将字符串变成多个字符串,切割。获取规则以往的子串 4、想要得到符合要求的字符串子串,获取。获取符合规则的字符串 */ public static void test_1() { String str = "我我...我我...我要.....要要....学学学........学学...编编编.......编程..程.程.程程.....程...程"; str = str.replaceAll("\\.+",""); str = str.replaceAll("(.)\\1+","$1"); System.out.println(str); } /* 192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 8.109.90.30 将ip地址进行地址段的排序 还按照字符串自然顺序排序,只要让它们每一段都是3位即可。 1、按照每一段需要的最多的0进行补齐,那么每一段就会至少有3位, 2、将每一段只保留3位。这样所有的ip地址都是每一段3位。 */ public static void ipSort() { String ips = "192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 128.109.90.30"; ips = ips.replaceAll("(\\d+)", "00$1"); System.out.println(ips); ips = ips.replaceAll("0*(\\d{3})", "$1"); System.out.println(ips); String[] arr = ips.split(" "); TreeSet<String> sets = new TreeSet<String>(); for(String s : arr) { sets.add(s); } for(String s : sets) { System.out.println(s.replaceAll("0*(\\d+)", "$1")); } } }
/* 网页爬虫 */ import java.io.*; import java.util.regex.*; import java.net.*; class RegexTest2 { public static void main(String[] args) throws Exception { getMails_1(); } public static void getMails_1() throws Exception { URL url = new URL("http://127.0.0.1:8080/webt1/mailpage.html"); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; String mailReg = "\\w+@\\w+(\\.\\w+)+"; Pattern p = Pattern.compile(mailReg); while((line = br.readLine()) != null) { Matcher m = p.matcher(line); while(m.find()) { System.out.println(m.group()); } } } /* 获取指定文档中的邮件地址 使用获取功能。Pattern Matcher */ public static void getMails() throws Exception { BufferedReader br = new BufferedReader(new FileReader("mailpage.txt")); String line = null; String mailReg = "\\w+@\\w+(\\.\\w+)+"; Pattern p = Pattern.compile(mailReg); while((line = br.readLine()) != null) { Matcher m = p.matcher(line); while(m.find()) { System.out.println(m.group()); } } } }