黑马程序员_正则表达式

一、
正则表达式:符合一定规则的表达式。
作用:用于专门操作字符串。
特点:用于一些特定的符号来表示一些代码操作。简化书写。
所以学习正则表达式,就是在学习一些特殊符号的使用
好处:可以简化对字符串的复杂操作。
弊端:符号定义越多,正则越长,阅读性越差。

具体操作功能:
1、匹配:String matches方法,用规则匹配整个字符串,只要有一处不符号规则,就匹配结束,返回false.

2、切割:String split();

3、替换:replaceAll();

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); } }

String的一些方法使用的就是封装过的正则。

正则表达式的第四个功能
4、获取:将字符串中符号规则的子串取出。

操作步骤:
1、将正则表达式封装成对象。
2、让正则对象和要操作的字符串相关联
3、关联后,获取正则匹配引擎
4、通过引擎对符合规则的子串进行操作,比如取出。

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()); } } } }


----------------------- android培训java培训、java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值