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

------- 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-->z
		String 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值