• 正则表达式练习


    import java.util.Arrays;
    
    public class RegexTest2 {
    
        public static void main(String[] args) {
    
    //        test_1();
            
    //        test_2();
            
            checkMail();
            
        }
        
        /*
         * 练习1:治口吃
         * "我我我...我我..我要....要..要要....要要..学学学...学编..编编编....编编程程...程程"
         * 还原成:我要学编程
         */
        public static void test_1(){
            
            String temp = "我我我...我我..我要....要..要要....要要..学学学...学编..编编编....编编程程...程程";
            
            //1.明确要使用替换  replaceAll(regex,String);
            
            //2.将点干掉
            temp = temp.replaceAll("\.+","" );
            
            //3.将叠词干掉
            temp = temp.replaceAll("(.)\1+", "$1");
            System.out.println(temp);
        }
        
        /*
         * 练习2:对ip地址进行排序
         * 192.168.1.200  10.10.10.10  3.3.50.3  127.0.0.1
         */
        
        public static void test_2(){
            
            String temp = "192.168.1.200  17.10.10.10  3.3.50.3  127.0.0.1";
            
            //排序出现问题了,是按照字典顺序排的序。
            //问题在哪呢?是因为每一个地址段的位数不对。
            //如果位数都是三位就可以了,只有补0了。
            //每一段的位数都不同呢,咋补?
            //干脆按照最大所需0的个数补齐,每一段都补两个0
            
            
            //补两个0
            temp = temp.replaceAll("(\d+)", "00$1");
    //        System.out.println(temp);
            
            //保留每段最后3位
            temp = temp.replaceAll("0*(\d{3})","$1");
    //        System.out.println(temp);
            
            String[] ips = temp.split(" +");
            Arrays.sort(ips);
            for(String ip:ips){
    //            System.out.println(ip);
                System.out.println(ip.replaceAll("0*(\d+)","$1"));
            }
            
        }
        
        /*
         *练习3:校验邮件地址
         */
        
        public static void checkMail(){
            
            String mail = "abc12@sina.com.cn";
            
            String regex = "\w+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,3}){1,3}";
            regex = "\w+@\w+(\.\w+)+";//1@1.1 比较笼统
            
            
            
            boolean b = mail.matches(regex);
            
            System.out.println(mail+":"+b);
        }
        
    
    }
  • 相关阅读:
    native-base中Input,Textarea等组件在ios平台下不能输入中文
    react-native中TextInput在ios平台下不能输入中文
    react-native android/ios 手动/自动 修改版本号
    react-native修改android包名
    React-Native——html/css
    去除npm run dev日志warn记录
    Python并发编程:多进程-互斥锁
    Python并发编程:多进程-守护进程
    Python并发编程:多进程-join方法
    面向对象练习题
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5929045.html
Copyright © 2020-2023  润新知