黑马程序员——正则表达式

来源:互联网 发布:cgss卡牌数据库 编辑:程序博客网 时间:2024/06/05 21:50

------- android培训、java培训、iOS培训、.Net培训期待与您交流! ----------


正则表达式:符合一定规则的表达式

作用:用于专门操作字符串

特点:用一些特定的符号来表示一些代码操作,这样就简化书写

所以学习正则表达式,就是在学习一些特殊符号的使用

好处:可以简化对字符串的复杂操作

弊端:符号定义越多,正则越长,阅读性越差

 

具体操作功能:

1.匹配:

String  matches方法用规则匹配整个字符串,只要有一处不符号规则,就匹配结束,返回false

class RegexDemo {public static void main(String[] args) {//demo();checkTel();}public static void demo(){//String str="a";//String regex="[bcd]";//b,c,d中的一个String str="az";String regex="[bcd][a-z]";//第一个字符为b,c,d中的一个,第二个字符为a-z(小写字母)//String regex="[a-zA-Z][0-9]";//第一个字符为字母,第二个字符为数字//String regex="[a-zA-Z]\\d";//第一个字符为字母,第二个字符为数字0-9,用\d表示,\转义//String regex="[a-zA-Z]\\d?";//第一个字符为字母,第二个字符为数字,要么有要么没有(\d?)//String regex="[a-zA-Z]\\d*";//第一个字符为字母,第二个字符为数字,0个或多个boolean b=str.matches(regex);System.out.println(b);}/*匹配手机号段只有13***   15***   18****/public static void checkTel(){String tel="13900001111";String telReg="1[358]\\d{9}";//定义规则//第一位1,第二位3,5,8,之后9个数字System.out.println(tel.matches(telReg));}}


2.切割:String split();

class RegexDemo {public static void main(String[] args) {splitDemo("zhangsan,lisi,wangwu",",");//定义规则,按,切splitDemo("zhangsan  lisi    wangwu"," +");//用一个或多个空格切splitDemo("zhangsan.lisi.wangwu","\\.");//.是个特殊符号,代表任意字符splitDemo("c:\\abc\\a.txt","\\\\");//一个\转义一个\//按照叠词完成切割,为了可以让规则的结果被重用,可以将规则封装成一个组,用()//完成,组的出现都有编号,从1开始,想要使用已有的组可以通过\n(n就是组的编号)的形式来获取splitDemo("erkktyqquizzo","(.)\\1");//第一位为任意字符用()封装成组,第二位与第一位一致,\1代表引用前面的结果splitDemo("erkktyqqquizzzzo","(.)\\1+");}public static void splitDemo(String str,String reg){String[] arr=str.split(reg);for(String s : arr){System.out.println(s);}}}


3替换String replaceAll();

class  RegexDemo{public static void main(String[] args) {//将字符串中的数字替换成#String str="wer137351786ty32795769uio9735735386f";//replaceAllDemo(str,"\\d{5,}","#");//将叠词替换成&String str1="fhkktyqqqfizzzzo";//replaceAllDemo(str1,"(.)\\1+","&");//将重叠的字符替换成单个字母  zzzz-->zString str2="fhkktyqqqfizzzzo";replaceAllDemo(str1,"(.)\\1+","$1");//用$获取组}public static void replaceAllDemo(String str,String reg,String newStr){str=str.replaceAll(reg,newStr);System.out.println(str);}}


4.获取将字符串中符合规则的子串取出

 

/*操作步骤:1.将正则表达式封装成对象2.让正则对象和要操作的字符串相关联3.关联后,获取正则匹配引擎4.通过引擎对符合规则的子串进行操作,比如取出*/import java.util.regex.*;class  RegexDemo{public static void main(String[] args) {getDemo();}public static void getDemo(){String str="ming tian jiu yao fang jia la,da jia.";String reg="\\b[a-z]{3}\\b";//   \b单词边界/*//验证String的String str="123456";String reg="[1-9]\\d{4,14}";*///将规则封装成对象Pattern p=Pattern.compile(reg);//没有构造函数,compile静态方法,类名调用,返回本类对象//让正则对象p和要作用的字符串相str关联,获取匹配器对象Matcher m=p. matcher(str);//返回引擎(或叫匹配器)//System.out.println(m.matches());//其实String类中的matches方法,用的就是Pattern和Matcher对象来完成的//只不过被String的方法封装后,用起来较为简单,但是功能却单一while(m.find())//将规则作用到字符串上,并进行符合规则的子串查找,返回boolean型{System.out.println(m.group());//group方法用于获取匹配后的结果System.out.println(m.start()+"..."+m.end());//打印匹配后的结果的角标位置}}}


网页爬虫:其实就是一段小程序,功能就是爬互联网上一些指定的信息

import java.util.regex.*;import java.io.*;import java.net.*;class  MailDemo{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/myweb/mail.html");URLConnection conn=url.openConnection();//获取连接器BufferedReader bufIn=new BufferedReader(new InputStreamReader(conn.getInputStream()));String line=null;String mailreg="\\w+@\\w+(\\.\\w+)+";Pattern p=Pattern.compile(mailreg);while((line=bufIn.readLine())!=null){Matcher m=p.matcher(line);while(m.find()){System.out.println(m.group());}}}/*获取指定文档中的邮件地址使用获取功能 Pattern Matcher*/public static void getMails() throws Exception{BufferedReader bufr=new BufferedReader(new FileReader("mail.txt"));String line=null;String mailreg="\\w+@\\w+(\\.\\w+)+";Pattern p=Pattern.compile(mailreg);while((line=bufr.readLine())!=null){Matcher m=p.matcher(line);while(m.find()){System.out.println(m.group());}}}}



0 0
原创粉丝点击