[Java视频笔记]day25

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

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

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

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

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

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

具体操作功能:

1. 匹配:String类中的matches方法,用规则匹配整个字符串,只要有一处不符合规则,就匹配结束,返回false

         [abc]该字符只能是a,b,c中的一个

         [^abc]该字符是除了a,b,c的其它任何字符

         [a-z]该字符是a到z中的一个

         [0-9]也可以用\d表示,在字符串中用\\d表示,因为得转义。

正则表达式中的\都是成对出现的\\

 

对QQ号码进行校验

要求:5~15位,0不能开头,只能是数字

   try

                   {

                            longl = Long.parseLong(qq);

                   }

                   catch(NumberFormatException e)

                   {

                            System.out.println("出现非法字符");

                   }//可以判断字符串是否只包含数字

用正则表达式:

class RegexDemo 
{
	public static void main(String[] args) 
	{
		checkQQ();
	}
	public static void checkQQ()
	{
		String qq = "4044593";
		String regex = "[1-9][0-9]{4,14}";
		//第一位是1到9之间的数,后面的位数在4到14之间且是0到9之间的数

		boolean flag = qq.matches(regex);

		if(flag)
			System.out.println(qq+"...合法");
		else
			System.out.println(qq+"...不合法");
	}
}

2. 切割:String split()

class RegexDemo 
{
	public static void main(String[] args) 
	{
		splitDemo("zhangan.lisi.wangwu", "\\.");
		//zhangan lisi wangwu
		splitDemo("c:\\abc\\a.txt", "\\\\");
		//c: abc a.txt
		splitDemo("erkktyqqquio", "(.)\\1+");//按照叠词切割
		//为了可以让规则的结果被重用,可以将规则封装成一个组,
		//用()完成,租的出现都有编号,从1开始,那么想要使用已有的组
		//可以通过\n的形式来获取,n是组的编号
	}

	public static void splitDemo(String str, String reg)
	{
		//String reg = " +";//按照多个空格进行切割
		//String reg = "\\.";//按照.切割
		String[] arr = str.split(reg);
		for(String s : arr)
		{
			System.out.println(s);
		}
	}
}


3. 替换  String replaceAll(String regex, String replacement)

class RegexDemo 
{
	public static void main(String[] args) 
	{
		String str = "dfj345454jiji43ji3454542";//将字符串中的数字替换成#
		replaceAllDemo(str, "\\d{5,}", "#");//输出 dfj#jiji43ji#

		String str1 = "erkkkkerewssssss";//将叠词替换成&
		replaceAllDemo(str1, "(.)\\1+", "&");//输出 er&erew&

		String str11 = "erkkkkerewssssss";//将重叠的字符替换成单个字母aaaaa-->a
		replaceAllDemo(str11, "(.)\\1+", "$1");//输出erkerews
	}

	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 = "have a good day now see ";
		String reg = "\\b[a-z]{3}\\b";//\b是单词边界

		//将规则封装成对象
		Pattern p = Pattern.compile(reg);

		//让正则对象和要作用的字符串相关联。
		//获取匹配器对象
		Matcher m = p.matcher(str);
		
		//System.out.println(m.matches());//这里的matches和String类的matches方法一样,
		//其中String类中的matches方法用的就是Pattern和Matcher对象来完成的,只不过被
		//String方法封装后,用起来较为简单,但功能单一

		//boolean b = m.find();//将规则作用到字符串上,并进行符合规则的子串查找
		//System.out.println(b);
		//System.out.println(m.group());//用于获取匹配后的结果。
		while(m.find())
		{
			System.out.println(m.group());//输出 day now see
			System.out.println(m.start()+"...."+m.end());//每个子串的索引位置包含头,不包含尾
		}
	}
}


练习:

需求:将下列字符串转成:我要学编程

我我…我我..我要…要要….要要….学学学…..学..编编编..编编…程..程程..程…程

 

到底用四种功能中的哪一个或者哪几个呢?

思考方式:

1. 如果只想知道该字符串是对是错,可以使用匹配。

2. 想要将已有的字符串变成另一个字符串,可以替换。

3. 想要按照指定的方式将字符串变成多个字符串,可以使用切割。获取规则以外的子串。

4. 想要获取拿到符合需求的字符串子串,就是获取。获取符合规则的子串。

 

需求:将ip地址进行地址段顺序的排序

192.68.1.254 102.49.23.013 10.10.10.102.2.2.2 8.109.90.30

 

思考方式:

还按照字符串自然顺序,只要让他们每一段都是3位即可。

1. 按照每一段需要最多的0进行补齐,那么每一段就会至少保证有3位。

2. 将每一段只保留3位。这样所有的ip地址都是每一段3位

 

需求:对邮件地址进行校验。

import java.util.*;

class RegexTest 
{
	public static void main(String[] args) 
	{
		//test_1();
		//ipSort();
		checkMail();
	}
	public static void test_1()
	{
		String str = "我我...我我..我..要要...要要...学学..学..编编编..编编..程..程程....程";
		/*
		将已有字符串变成另一个字符串,使用替换功能
		1. 可以先将 . 去掉
		2. 在将多个重复的内容变成单个内容
		*/
		str = str.replaceAll("\\.+", "");
		str = str.replaceAll("(.)\\1+", "$1");
		System.out.println(str);
	}

	public static void ipSort()
	{
		String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
		ip = ip.replaceAll("(\\d+)", "00$1");
		//System.out.println(ip);
		//输出00192.0068.001.00254 00102.0049.0023.00013 0010.0010.0010.0010 002.002.002.002 008.00109.0090.0030
		
		ip = ip.replaceAll("0*(\\d{3})", "$1");
		//System.out.println(ip);
		//输出192.068.001.254 102.049.023.013 010.010.010.010 002.002.002.002 008.109.090.030
		
		String[] arr = ip.split(" ");
		TreeSet<String> ts = new TreeSet<String>();
		for(String s : arr)
		{
			ts.add(s);
		}
		for(String s : ts)
		{
			System.out.println(s.replaceAll("0*(\\d+)", "$1"));//去掉前缀0
		}
		//输出
		//2.2.2.2
		//8.109.90.30
		//10.10.10.10
		//102.49.23.13
		//192.68.1.254
	}

	public static void checkMail()
	{
		String mail = "abc12@sina.com";

		String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//较为精确的匹配
		//reg="\\w+@\\w+(\\.\\w+)+";//相对不太精确的匹配

		System.out.println(mail.matches(reg));
	}
}

网页爬虫(蜘蛛)

import java.io.*;
import java.util.regex.*;
import java.net.*;

class RegexTest2 
{
	public static void main(String[] args) throws Exception
	{
		getMails();
	}

	//获取指定文档中的邮件地址
	//使用获取功能 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());
			}
		}
	}
	public static void getMails_1() throws Exception//从网页中获取
	{
		URL url = new URL("www.baidu.com");
		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());
			}
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值