java_正则表达式的3个例子

package cn.itcast.regextest.demo;

import java.util.HashSet;

public class TestDemo {

	public static void main(String[] args) {
//		test1();
//		test2();
		test3();
	}

	public static void test3() {
		/**
		 * 邮箱地址校验
		 * 
		 * */
		String mail="[email protected]";
		boolean b=mail.matches("[a-zA-Z_]+\\@[a-zA-Z_]+\\.([a-zA-Z]{1,3})+");
		System.out.println(mail+":"+b);

	}

	public static void test2() {
		/**
		 * ip地址切割:ip=192.168.1.0  3.5.6.4  168.16.7.64
		 * 
		 * */
		
//		1、对ip进行补0;
		String ip="192.168.1.0   3.5.6.4    168.16.7.64";
		ip=ip.replaceAll("(\\d+)", "00$1");
		
//		3、保留后三位
		ip=ip.replaceAll("0*(\\d{3})", "$1");
		
//		3、把每个ip切割出来;
		String[] ips=ip.split(" +");
		HashSet<String>hs=new HashSet<String>();
		for(String ipp:ips) {
			hs.add(ipp);
		}
		for(String jpp:hs) {
//			4、去掉多余的零
			jpp=jpp.replaceAll("0*(\\d+)", "$1");
			System.out.println(jpp);
		}
		
	}

	public static void test1() {
		String str="我我....我我..我我...我要..要要..要.要要.要回回...回回..回回....回回..回回..回回家...家家...家家...家家.家";
		str=str.replaceAll("\\.", "");
		str=str.replaceAll("(.)\\1+", "$1");
		
		System.out.println(str);
		
	}

}

猜你喜欢

转载自blog.csdn.net/TDOA1024/article/details/82900817
今日推荐