正则表达式经典实例
正则表达式经典实例
正则表达式的常用操作: 第一题 匹配电话号码 public static void piPei() { String tel = "13534781023"; String regex = "1[34578]d{9}"; boolean b = tel.matches(regex); System.out.println(b); } 第二题 已知字符串:zhanngsan***lisi&&&&&wangwu######zhaoliugwwwwwwwzhouqi,把之中的人名获取出来。 public static void qieGe() { String str = "zhanngsan***lisi&&&&&wangwu######zhaoliugwwwwwwwzhouqi"; //重复字母中的第一位是任意字符,第二位跟第一位一样,然后可以有多个 String regex = "(.)1+"; String[] arr = str.split(regex); for(String ss:arr) { System.out.println(ss); } }
第三题 已知字符串:zhangsan***lisi&&&&&wangwu######liunengwwwwwwwxieyingwu,去除重复字母 已知字符串:woeiuoie3457948herehk09856909457809sdfhsdkjfh5987689762344,去除8位以上的数字 已知字符串:18620982678,把手机号码中间4位用*代替 public static void tiHuan() { //去除重复字母 String str1 = "zhangsan***lisi&&&&&wangwu######liunengwwwwwwwxieyingwu"; str1 = str1.replaceAll("(.)1+","$1");//$1 表示取正则表达式中第一组的值 System.out.println(str1); //去除8位以上的数字 String str2 = "woeiuoie3457948herehk09856909457809sdfhsdkjfh5987689762344"; str2 = str2.replaceAll("d{8,}","****"); System.out.println(str2); //把手机号码中间4位用*代替 String str3 = "18620982678"; str3 = str3.replaceAll("(d{3})d{4}(d{4})","$1****$2"); System.out.println(str3); } 输出结果是: 第四题 已知字符串:I like you very much.获取4个字母组成的单词 public static void huoQu() { String str = "I like you very much."; String regex = "[a-z]{4}"; //把字符串形式的正则表达式编译成Patter类型的对象 //Pattern类型的对象不具备获取的功能 Pattern pattern = Pattern.compile(regex); //Matcher对象的作用是:从str中获取符合pattern规则的内容 Matcher m = pattern.matcher(str); //使用Matcher对象获取 while(m.find()) { System.out.println(m.group()); } }
第五题 已知字符串:我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程,要求,转成:我要学编程 public static void main(String[] args) { String str = "我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程"; //先去除. str = str.replaceAll(".+",""); System.out.println(str); //在去除重复汉字 str = str.replaceAll("(.)1+","$1"); System.out.println(str); } 第六题 对ip地址按照数值顺序排序。 public static void main(String[] args) { String str = "192.168.1.200 10.10.10.10 4.4.4.4 127.0.0.1"; //每个值前边儿补2位0 str = str.replaceAll("(d{1,3})","00$1"); System.out.println(str); //每个值保留3位 str = str.replaceAll("0*(d{3})","$1"); System.out.println(str); //按照空格切割出每个IP地址 String[] arr = str.split(" +"); Arrays.sort(arr); for(String ss:arr) { System.out.println(ss.replaceAll("0*(d{1,"$1")); } }
第七题 对邮件地址进行匹配 public static void main(String[] args) { String email = "lisi_001@sina.com.cn"; //String regex = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(.[a-zA-Z]+){1,2}"; String regex = "w+@w+(.w+)+"; System.out.println(email.matches(regex)); }
第八题 网页爬虫,获取网页上的邮箱 //从网络上的网页获取邮箱 public static void getEmail() throws IOException { String path = "http://localhost:8080/myweb/mail.html"; URL url = new URL(path); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream();//读取服务器端返回的mail.html文件的数据 BufferedReader br = new BufferedReader(new InputStreamReader(in)); String regex = "w+@w+(.w+)+"; Pattern p = Pattern.compile(regex); String line = null; while((line = br.readLine())!=null) { //使用Matcher对象从读取的一行中获取符合邮箱规则的内容 Matcher m = p.matcher(line); while(m.find()) { System.out.println(m.group()); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |