正则表达式-java

前端之家收集整理的这篇文章主要介绍了正则表达式-java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package regexUtilTest;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexString {
	//1 匹配
	public boolean Matches(String src,String regex){
		boolean flag=false;
		if(src.matches(regex))
			flag=true;
		else
			flag=false;
		return flag;
	}
	//切割
	public String [] Split(String src,String reg){
		String a[] = src.split(reg);
		for (String string : a) {
			System.out.println(string);
		}
		return a;
	}
	
	//替换
	public String Replace(String src,String regex,String newstr){
		String newstring = 	src.replaceAll(regex,newstr);	
		System.out.println(newstring);
		return newstring;
	}
	
	
	

	//获取
	public void Get(String src,String regex){
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(src);
		while(m.find()){
			System.out.println(m.group());
			System.out.println(m.start()+":"+m.end());
			
		}
		
	}
	
	public static void main(String[] args) {
		RegexString regexString = new RegexString();
		String qq= "^[1-9]\\d{5,14}$";  
		String phone = "^(\\+86 )?1[358]\\d{9}$";  
		String str="wangtianyieeexiaolieeeezhaotianyugggglkjkj";
		String regex="(.)\\1+";
		String src = "13533828340";
		
		
		regexString.Replace(str,regex,"#");
		
		regexString.Replace(str,"$1");
		
		//需求 将手机号码中间几位数屏蔽
		regexString.Replace("15802397891","(\\d{3})(\\d{4})(\\d{4})","$1****$3");
		//regexString.Split(str,regex);
		
		
		//需求:取出3个字母的单词
		String src1="cheng xu yuan,yi sheng hei";
		String regex1="\\b[a-zA-Z]{3}\\b";
		regexString.Get(src1,regex1);
		
		//需求 替换叠词
		String question1 ="aaa....ccccc.........dddd.......eeeee....ccccc........ggggg.....eeewwwww.....";
		//首先替换掉.
		String q1=regexString.Replace(question1,"\\.+","" );
		//然后替换掉重复字符
		regexString.Replace(q1,"(.)\\1+","$1");
		
		//需求:匹配ip分租    并 按照ip 排序 
		String ip = "172.168.5.34  22.22.245.123   33.22.33.1   2.43.56.113 224.22.3.53 ";
		//考虑到要排序可以用TreeSet排序 但是22.22.245.123 如果用treeset排序 就会比172.....1开头的打大 所以需要把所有ip转换成3位数 
		//因为ip每个.间的位数不同 将每个数字前加上两个0 则ip至少是3位以上
		String ip1 = ip.replaceAll("(\\d+)","00$1");
		//然后将多位的ip替换成3位
		ip1 = ip1.replaceAll("0*(\\d{3})","$1");
		System.out.println(ip1);
		//排序
		String ips[]=regexString.Split(ip1," +");
		TreeSet<String> ts =new TreeSet<String>();	
		for (String string : ips) {
			ts.add(string);
		}
		//输出
		System.out.println("---------------");
		for (String string : ts) {
			string=string.replaceAll("0*(\\d{1,3})","$1");
			System.out.println(string);
		}
		
		//匹配邮箱  注意下划线
		String mail = "eico@sin-omcn.com";
		String regexMail="(w{3}\\.)?[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_]+)+";
		String regexMail2="\\w+@\\w+\\-\\w+(\\.\\w+)+";	
		
		System.out.println(mail.matches(regexMail));
		System.out.println(mail.matches(regexMail2));
		

		String http = "^'http://\\w+(\\.\\w+)+(/[a-zA-Z0-9_-]+)*(\\.\\w+)*'$";
		String httpsrc="\"http://www.bilibili.com/html/ma_pc.html\"";
		System.out.println(httpsrc);
		System.out.println(httpsrc.matches(http));
		
		//需求:将a3ab2de5将数字之间的字符重复数字那么多次
		String qunaer = "a3ab2de5";
		String relexq="([a-z]+\\d)";
		//1在每个组之间添加一个#号便于分租
		String qunaer2=qunaer.replaceAll(relexq,"$1#");
		//2把它分成多个组
		String[] b=qunaer2.split("#");
		//3new一个可变长度的builder对象
		StringBuilder finalbuilder=new StringBuilder();
		for (String string : b) {
			Pattern p = Pattern.compile("\\d");
			Matcher m = p.matcher(string);
			int copy=0;
			while(m.find()){
				copy = Integer.parseInt(m.group());
				System.out.println(copy);
			}
			String newstr=string.replaceAll("([a-z]+)\\d","$1");
			StringBuilder builder =new StringBuilder(newstr);
			for (int j = 0; j < copy-1; j++) {					
				builder.append(newstr);
			}
			finalbuilder = finalbuilder.append(builder);
		}
		System.out.println(finalbuilder);
		
	}
	
}
原文链接:https://www.f2er.com/regex/360601.html

猜你在找的正则表达式相关文章