码迷,mamicode.com
首页 > 其他好文 > 详细

正则练习

时间:2018-02-10 01:22:38      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:nbsp   for   print   gpo   out   split   color   arrays   log   

练习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);            
        }    

 

正则练习

标签:nbsp   for   print   gpo   out   split   color   arrays   log   

原文地址:https://www.cnblogs.com/exexex/p/8437160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!