JAVA--正则表达式练习总结

正则练习总结

正则表达式四种操作方式的选择方式

       1.如果只想知道该字符是否对或者错,使用匹配.

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

       3.想要按照自定的方式将字符串变成多个字符串.切割获取规则以外的子串.

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

1 检验字符串

/*
	需求:对邮件地址进行校验。
	*/
	public static void checkMail(){
		String mail="abc12@sina.com";
		
		// 较为精确验证邮箱正则表达式
		String reg="\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
		// 相对不太精确的匹配
		reg="\\w+@\\w+(\\.\\w+)+";
		
		System.out.println(mail.matches(reg));
	}

2 IP地址排序

/*
	需求:将ip地址进行地址段顺序的排序,还要按照字符串自然顺序。
	思路:
	1.按照每一段需要的最多的0进行补齐,那么每一段就会至少保证有3位。
	2.将每一段只保留3位,这样所有的ip地址都是每一段3位。
	3.通过TreeSet集合存储并排序
	4.去除数字前边的零,并打印结果
	*/
	public static void isSort(){
		String ip="192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 8.109.90.30";
		// 补零
		ip=ip.replaceAll("(\\d+)","00$1");
		System.out.println(ip);
		// 去零,保证都是3位
		ip=ip.replaceAll("0*(\\d{3})","$1");
		System.out.println(ip);
		// 分割
		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"));
		}
	}

3 邮箱地址验证

/*
	需求:对邮件地址进行校验。
	*/
	public static void checkMail(){
		String mail="abc12@sina.com";
		
		// 较为精确验证邮箱正则表达式
		String reg="\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
		// 相对不太精确的匹配
		reg="\\w+@\\w+(\\.\\w+)+";
		
		System.out.println(mail.matches(reg));
	}

4 网络爬虫:获取指定文本中的邮件地址

/*
	获取指定文档中的邮件地址。
	使用获取功能。 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());
			}
		}
		
	}

5 网络爬虫:获取指定网页中的邮件地址

/*
	获取指定地址网页中的邮件地址
	*/ 
	public static void getMails_1()throws Exception{
        // 指定网页地址
		URL url=new URL("https://zhidao.baidu.com/question/33050837.html");
		
		URLConnection conn=url.openConnection();
		
		BufferedReader bufIn=new BufferedReader(new InputStreamReader(conn.getInputStream()));
		PrintWriter pw = new PrintWriter(new FileWriter("mail.txt"), true);
		
		String line=null;
		
		String mailreg="\\w+@\\w+(\\.\\w+)+"; 
		// String mailreg="\\w+@\\w+(\\.[a-zA-Z]+)+"; 
		Pattern p=Pattern.compile(mailreg);
		
		while((line=bufIn.readLine())!=null){

		Matcher m=p.matcher(line);
			while(m.find()){
				// System.out.println(m.group());	// 打印右键地址
				pw.println(m.group()+ ";");	//把获取的右键地址写入到指定文件中
			}
		}
		pw.close();
		System.out.println("……获取完毕!");
	}

常用正则表达式

https://blog.csdn.net/zpz2411232428/article/details/83549502

https://blog.csdn.net/kiss_vicente/article/details/8050816

https://www.cnblogs.com/coder-wzr/p/7838527.html

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值