邮箱,ip,叠词的正则处理方式

package com.regexTest;

import java.util.TreeSet;

public class RegexTest {
    public static void main(String[] args) {
//        test1();//验证叠词
//        test2();//验证ip
        test3();//对邮箱经行匹配
    }

    private static void test3() {
        //对邮件地址经行效验
        String mail="abc12@sina.com";
        //较为精确的匹配
        String reg="[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\.[a-zA-Z]+){1,3}";
        //相对不太精确的匹配
        reg="\w+@\w+(\.\w+)+";
        System.out.println(mail.matches(reg));
    }

    private static void test2() {
        String ip="192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
        //补齐动作
        ip=ip.replaceAll("(\d+)", "00$1");
        //替换
        ip=ip.replaceAll("0*(\d{3})", "$1");
        //切割
        String [] arr=ip.split(" ");
        //存在一个集合中,经行排序
        TreeSet<String> ts=new TreeSet<String>();
        for (String string : arr) {
            ts.add(string);
        }
        for (String string : ts) {
            System.out.println(string.replaceAll("0*(\d+)", "$1"));
        }
        
    }

    private static void test1() {
        String str="我我...我我...我要...要要...要要...学学学....学学...编编编...编程..程.程程...程...程";
        String regex="\.{1,}";
        //先去除所有的点
        str=str.replaceAll(regex, "");
        
        //去除叠词
        str=str.replaceAll("(.)\1+", "$1");
        System.out.println(str);
        
    }
    
}
原文地址:https://www.cnblogs.com/boyhan/p/6445795.html