加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式经典实例

发布时间:2020-12-14 01:02:10 所属栏目:百科 来源:网络整理
导读:正则表达式经典实例 正则表达式的常用操作: * 1:匹配: * 使用的是String类中的boolean matches(String regex)方法 * 2:切割: * 使用的是String类中的String[] split(String regex) * 3:替换: * String replaceAll(String regex,String replacement) * 4

正则表达式经典实例

正则表达式的常用操作:
* 1:匹配:
* 使用的是String类中的boolean matches(String regex)方法
* 2:切割:
* 使用的是String类中的String[] split(String regex)
* 3:替换:
* String replaceAll(String regex,String replacement)
* 4:获取:
* 从一个字符串得到所有符合某个正则规则的内容
* Pattern
* Matcher

第一题

匹配电话号码

public static void piPei()
	{
		String tel = "13534781023";
		String regex = "1[34578]d{9}";
		
		boolean b = tel.matches(regex);
		System.out.println(b);
	}

第二题

已知字符串:zhanngsan***lisi&&&&&wangwu######zhaoliugwwwwwwwzhouqi,把之中的人名获取出来。

public static void qieGe()
	{
		String str = "zhanngsan***lisi&&&&&wangwu######zhaoliugwwwwwwwzhouqi";
	
		//重复字母中的第一位是任意字符,第二位跟第一位一样,然后可以有多个
		
		String regex = "(.)1+";
		
		String[] arr = str.split(regex);
		for(String ss:arr)
		{
			System.out.println(ss);
		}
	}


第三题

已知字符串:zhangsan***lisi&&&&&wangwu######liunengwwwwwwwxieyingwu,去除重复字母

已知字符串:woeiuoie3457948herehk09856909457809sdfhsdkjfh5987689762344,去除8位以上的数字

已知字符串:18620982678,把手机号码中间4位用*代替

public static void tiHuan()
	{
		//去除重复字母
		String str1 = "zhangsan***lisi&&&&&wangwu######liunengwwwwwwwxieyingwu";
		str1 = str1.replaceAll("(.)1+","$1");//$1 表示取正则表达式中第一组的值
		System.out.println(str1);
		
		//去除8位以上的数字
		String str2 = "woeiuoie3457948herehk09856909457809sdfhsdkjfh5987689762344";
		str2 = str2.replaceAll("d{8,}","****");
		System.out.println(str2);
		
		//把手机号码中间4位用*代替
		String str3 = "18620982678";
		str3 = str3.replaceAll("(d{3})d{4}(d{4})","$1****$2");		
		System.out.println(str3);
	}

输出结果是:
zhangsan*lisi&wangwu#liunengwxieyingwu
woeiuoie3457948herehk****sdfhsdkjfh****
186****2678

第四题

已知字符串:I like you very much.获取4个字母组成的单词

public static void huoQu()
	{
		String str = "I like you very much.";
		String regex = "[a-z]{4}";
		
		//把字符串形式的正则表达式编译成Patter类型的对象
		//Pattern类型的对象不具备获取的功能
		Pattern pattern = Pattern.compile(regex);
		
		//Matcher对象的作用是:从str中获取符合pattern规则的内容
		Matcher m = pattern.matcher(str);
		
		//使用Matcher对象获取
		while(m.find())
		{
			System.out.println(m.group());
		}	
	}


第五题

已知字符串:我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程,要求,转成:我要学编程

public static void main(String[] args) {
		
		String str = "我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程";
		
		//先去除.
		str = str.replaceAll(".+","");
		System.out.println(str);
		//在去除重复汉字
		str = str.replaceAll("(.)1+","$1");
		System.out.println(str);
	}

第六题

对ip地址按照数值顺序排序。
192.168.1.2 10.10.10.10 4.4.4.4 127.0.0.1

public static void main(String[] args) {
		String str = "192.168.1.200  10.10.10.10  4.4.4.4 127.0.0.1";
		
		//每个值前边儿补2位0
		str = str.replaceAll("(d{1,3})","00$1");
		System.out.println(str);
		
		//每个值保留3位
		str = str.replaceAll("0*(d{3})","$1");
		System.out.println(str);
		
		//按照空格切割出每个IP地址
		String[] arr = str.split(" +");
		Arrays.sort(arr);
		
		for(String ss:arr)
		{
			System.out.println(ss.replaceAll("0*(d{1,"$1"));
		}

	}


第七题

对邮件地址进行匹配

public static void main(String[] args) {
		
         String email = "lisi_001@sina.com.cn";
         
         //String regex = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(.[a-zA-Z]+){1,2}";
         
         String regex = "w+@w+(.w+)+";
         
         System.out.println(email.matches(regex));
	}


第八题

网页爬虫,获取网页上的邮箱

//从网络上的网页获取邮箱
	public static void getEmail() throws IOException
	{
		String path = "http://localhost:8080/myweb/mail.html";
		
		URL url = new URL(path);
		
		URLConnection conn = url.openConnection();
		
		InputStream in = conn.getInputStream();//读取服务器端返回的mail.html文件的数据
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		String regex = "w+@w+(.w+)+";
		Pattern p = Pattern.compile(regex);
		
		String line = null;
		while((line = br.readLine())!=null)
		{
			//使用Matcher对象从读取的一行中获取符合邮箱规则的内容
			Matcher m = p.matcher(line);
			while(m.find())
			{
				System.out.println(m.group());
			}
		}
		
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读