正则练习

练习1

String str= "我我我   要要要   要 学学学   编编编   编编 编程程程 程程。";
        String regex=" +";
        str=str.replaceAll(regex, "");
        regex ="(.)\1+";
        str=str.replaceAll(regex, "$1");
        System.out.println(str);

练习2

//将下列ip排序
        String str= "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
        //在前面加俩零
        str=str.replaceAll("(\d+)","00$1" );
        //取后三位
        str=str.replaceAll("0*(\d{3})","$1" );
        //切割
        
        String[] ips =  str.split(" +");
        Arrays.sort(ips);
        for(String ip :ips)
        {
            ip=ip.replaceAll("0*(\d+)", "$1");            
            System.out.println(ip);            
        }    
原文地址:https://www.cnblogs.com/exexex/p/8437160.html