Java基础<十五>_正则表达式

正则表达式

一、正则表达式

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

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

    好处;可以简化对字符串的复杂操作,具有不可替代的作用。

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

    特点:用一些特定的符号表示一些代码操作,简化书写,所以学习正则表达式就是学习一些特殊符号的使用。

    具体操作的功能:

    1、匹配:boolean matches(String regex);

    2、切割:String[] split(String regex);

    3、替换:String replaceAll(String regex,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串;

    4、获取:将字符串中符合规则的子串取出。

    操作步骤:

    1、将正则表达式封装成为对象;

    2、让正则对象和要操作的字符串相关联;

    3、关联后,获取正则匹配引擎;

    4、通过引擎对符合规则的子串进行操作,例如:取出。

    到底该用哪个功能?

    思路:

    1、如果只想知道该字符串是否对错,使用匹配

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

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

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

下面代码演示:

1、匹配、切割、替代:

package itheima.day25;

public class RegexDemo {

	public static void main(String[] args) {
		
//		checkQQ_3();
	
//		checkTel();
		
//		splitDemo("zs,ls,ww,zl,zq",",");
		
//		splitDemo("zs   ls ww    zl  zq"," +");//+:一次或者多次
	
//		splitDemo("zs.ls.ww.zl.zq","\\.");
		
//		splitDemo("c:\\abc\\b.txt","\\\\");
		
//		为了让规则重用,可以使用组的概念
//		splitDemo("zsaaalsddw1wvvvzlrrzq","(.)\\1+");//按照叠词切割
	
		String str = "dsfds15869563256dsg843631536";//将字符串中的数字替换成为#
		replaceDemo(str,"\\d{5,}","#");
		
		String str1 = "zsaaalsddw1wvvvzlrrzq";//将叠词替换成为#
//		replaceDemo(str1,"(.)\\1+","#");
		
//		将叠词替换成为一个单词
		replaceDemo(str1,"(.)\\1+","$1");
		
	}
	
//	对QQ号码进行校验,要求:5--15,0不能开头,只能是数字
	public static void checkQQ_3(){
		
		String qq = "84a3631536";
//		String regex = "[1-9][0-9]{4,14}";
		String regex = "[1-9]\\d{4,14}";
		
//		匹配
		boolean flag = qq.matches(regex);
		if(flag)
			System.out.println(qq+"::::匹配");
		else
			System.out.println(qq+"::::不匹配");
	}
	
//	匹配手机号:13XXX,15XXX,18XXX
	public static void checkTel(){
		
		String tel = "15829962412";
		String telRegex = "1[358]\\d{9}";
		
//		匹配
		boolean flag = tel.matches(telRegex);
		if(flag)
			System.out.println(tel+"....是一个手机号");
		else
			System.out.println(tel+"....不是一个手机号");
	}
	
//	切割
	public static void splitDemo(String str,String reg){
		
		String[] arr = str.split(reg);
		for(String s: arr){
			System.out.println(s);
		}
	}
	
//	替代
	public static void replaceDemo(String str,String reg,String newStr){
		
		str = str.replaceAll(reg, newStr);
		System.out.println(str);
	}
}

2、获取:

package itheima.day25;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo2 {

	public static void main(String[] args) {
		getDemo();
	}
//	获取,符合规则
	public static void getDemo(){

		String str = "da jia,ming tian jiu yao fang jia le";
		String reg ="\\b[a-zA-Z]{3}\\b";//\b:单词边界
		
//		1、将正则表达式封装成为对象
		Pattern p = Pattern.compile(reg);
		
//		2、让正则对象和要作用的字符串相关联,获取匹配器对象
		Matcher m = p.matcher(str);
		
//		3、将规则作用到字符串上,并进行符合规则的子串查询
		while(m.find())
		{
//			4、用户获取匹配后的结果
			System.out.println(m.group());	
			System.out.println(m.start()+"......"+m.end());
		}		
	}	
}

3、练习:治口吃、ip排序、检验邮箱:

package itheima.day25;

import java.util.TreeSet;

public class RegexText {

	public static void main(String[] args) {
//		test_2();
		checkMail();
	}
	
//	将已有字符串变成另一个字符串,使用替换
//	  1、先将.去掉
//	  2、将多个重复的内容变成单个内容
	public static void test_1(){
		String str = "我我我.....要要要要.....学.....学学...编编....编编编.....程程程程程程";
		str = str.replaceAll("\\.+", "");
		str = str.replaceAll("(.)\\1+","$1");
		System.out.println(str);
	}
	
//	将ip地址进行地址段顺序的排序
//	 1、按照每一段需要的最多的0进行补齐,那么每一段就至少保证3位
//	 2、将每一段只保留3位,
	public static void test_2(){
		String ip = "192.68.1.254  1.2.49.34.13  10.11.12.13  8.9.14.63  254.168.102.210";
		
		ip = ip.replaceAll("(\\d+)", "00$1");
		ip = ip.replaceAll("0*(\\d{3})", "$1");
		String[] arr = ip.split(" +");
		TreeSet<String> ts = new TreeSet<String>();
		
		for(String ipp:arr){
			ts.add(ipp);
		}
		for(String ipp:ts){
			System.out.println(ipp.replaceAll("0*(\\d+)","$1"));
		}
	}
	
//	需求:对邮件地址进行校验
	public static void checkMail(){
		
		String  mail = "liyua_nq@163.com";
		String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+){1,3}";
		System.out.println(mail.matches(reg));	
	}	
}

4、练习:模拟网页爬虫:

package itheima.day25;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//网页爬虫(网络蜘蛛)
public class RegexTest2 {

	public static void main(String[] args) throws IOException {
		getMails();
	}
	
	public static void getMails() throws IOException{
//		用一个文件模拟一个网页
		BufferedReader bufr = 
				new BufferedReader(new FileReader("mail.txt"));
		
		String line  = null;
//		匹配邮箱的字符串
		String mailreg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+){1,3}";
//		将正则表达式封装成对象
		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 IOException{
		
		URL url =  new URL("");
		URLConnection conn = url.openConnection();
		
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		
		String line = null;

		String mailreg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+){1,3}";
		
		Pattern p = Pattern.compile(mailreg);
		
		while((line = bufIn.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、付费专栏及课程。

余额充值