栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA常用正则表达式(邮箱、链接、中英文、IP地址等)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA常用正则表达式(邮箱、链接、中英文、IP地址等)

import java.util.regex.Pattern;


public class PatternUtil {
    private static final Pattern email = Pattern.compile("\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}");
    private static final Pattern url = Pattern.compile("[a-zA-z]+://[^\s]*");
    private static final Pattern english = Pattern.compile("^[A-Za-z0-9]+$");
    private static final Pattern chinese = Pattern.compile("^[\u4e00-\u9fa5]{0,}$");
    private static final Pattern phone = Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
    private static final Pattern qq = Pattern.compile("^[1-9][0-9]{4,10}$");
    private static final Pattern wechat = Pattern.compile("^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$");
    private static final Pattern ipv4 = Pattern.compile("^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$");
    private static final Pattern ipv6 = Pattern.compile( "^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");

    public static boolean matchEmail(String src) {
        if (src == null) return false;
        return email.matcher(src).matches();
    }

    public static boolean matchUrl(String src) {
        if (src == null) return false;
        return url.matcher(src).matches();
    }

    public static boolean matchEnglish(String src) {
        if (src == null) return false;
        return english.matcher(src).matches();
    }

    public static boolean matchChinese(String src) {
        if (src == null) return false;
        return chinese.matcher(src).matches();
    }

    public static boolean matchPhone(String src) {
        if (src == null) return false;
        return phone.matcher(src).matches();
    }

    public static boolean matchQQ(String src) {
        if (src == null) return false;
        return qq.matcher(src).matches();
    }

    public static boolean matchWechat(String src) {
        if (src == null) return false;
        return wechat.matcher(src).matches() || matchPhone(src);
    }

    public static boolean matchIPV4(String src) {
        if (src == null) return false;
        return ipv4.matcher(src).matches();
    }

    public static boolean matchIPV6(String src) {
        if (src == null) return false;
        return ipv6.matcher(src).matches();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号