邮箱,手机号,身份证号等正则表达式的判断

前端之家收集整理的这篇文章主要介绍了邮箱,手机号,身份证号等正则表达式的判断前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.yzkj.utils.common;

@H_301_8@/** @H_301_8@ * Created by Administrator on 2017/1/23. @H_301_8@ */ @H_301_8@ import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class RegexUtils {


    @H_301_8@/** @H_301_8@ * 007 @H_301_8@ * 验证Email @H_301_8@ * 008 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@email @H_301_8@email地址,格式:[email]zhang@gmail.com[/email],[email]zhang@xxx.com.cn[/email],xxx代表邮件服务商 @H_301_8@ * 009 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 010 @H_301_8@ */ @H_301_8@ public static boolean checkEmail(String email) {

        String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";

        return Pattern.matches(regex,email);

    }

    @H_301_8@/** @H_301_8@ * 017 @H_301_8@ * 验证身份证号码 @H_301_8@ * 018 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@idCard @H_301_8@居民身份证号码15位或18位,最后一位可能是数字或字母 @H_301_8@ * 019 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 020 @H_301_8@ */ @H_301_8@ @H_301_8@ public static boolean checkIdCard(String idCard)

    {
        String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
        return Pattern.matches(regex,idCard);
    }

    @H_301_8@/** @H_301_8@ * 029 @H_301_8@ * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港)) @H_301_8@ * 030 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@mobile @H_301_8@移动、联通、电信运营商的号码段 @H_301_8@ * 031 @H_301_8@ * 移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡) @H_301_8@ * 032 @H_301_8@ * 、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用) @H_301_8@ * 033 @H_301_8@ * 联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g) @H_301_8@ * 034 @H_301_8@ * 电信的号段:133、153、180(未启用)、189 @H_301_8@ * 035 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 036 @H_301_8@ */ @H_301_8@ @H_301_8@ public static boolean checkMobile(String mobile) {

        String regex = "(\\+\\d+)?1[3458]\\d{9}$";

        return Pattern.matches(regex,mobile);

    }

    @H_301_8@/** @H_301_8@ * 043 @H_301_8@ * 验证固定电话号码 @H_301_8@ * 044 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@phone @H_301_8@电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447 @H_301_8@ * 045 @H_301_8@ * 国家(地区) 代码 :标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字, @H_301_8@ * 046 @H_301_8@ * 数字之后是空格分隔的国家(地区)代码@H_301_8@ * 047 @H_301_8@ * 区号(城市代码):这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号—— @H_301_8@ * 048 @H_301_8@ * 对不使用地区或城市代码的国家(地区),则省略该组件。 @H_301_8@ * 049 @H_301_8@ * 电话号码:这包含从 0 到 9 的一个或多个数字 @H_301_8@ * 050 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 051 @H_301_8@ */ @H_301_8@ public static boolean checkPhone(String phone) {
        String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";

        return Pattern.matches(regex,phone);

    }

    @H_301_8@/** @H_301_8@ * 058 @H_301_8@ * 验证整数(正整数和负整数) @H_301_8@ * 059 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@digit @H_301_8@一位或多位0-9之间的整数 @H_301_8@ * 060 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 061 @H_301_8@ */ @H_301_8@ @H_301_8@ public static boolean checkDigit(String digit) {
        String regex = "\\-?[1-9]\\d+";
        return Pattern.matches(regex,digit);
    }

    @H_301_8@/** @H_301_8@ * 068 @H_301_8@ * 验证整数和浮点数(正负整数和正负浮点数) @H_301_8@ * 069 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@decimals @H_301_8@一位或多位0-9之间的浮点数,如:1.23,233.30 @H_301_8@ * 070 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 071 @H_301_8@ */ @H_301_8@ public static boolean checkDecimals(String decimals) {

        String regex = "\\-?[1-9]\\d+(\\.\\d+)?";

        return Pattern.matches(regex,decimals);

    }

    @H_301_8@/** @H_301_8@ * 078 @H_301_8@ * 验证空白字符 @H_301_8@ * 079 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@blankSpace @H_301_8@空白字符,包括:空格、\t、\n、\r、\f、\x0B @H_301_8@ * 080 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 081 @H_301_8@ */ @H_301_8@ public static boolean checkBlankSpace(String blankSpace) {

        String regex = "\\s+";

        return Pattern.matches(regex,blankSpace);
    }

    @H_301_8@/** @H_301_8@ * 088 @H_301_8@ * 验证中文 @H_301_8@ * 089 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@chinese @H_301_8@中文字符 @H_301_8@ * 090 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 091 @H_301_8@ */ @H_301_8@ public static boolean checkChinese(String chinese) {

        String regex = "^[\u4E00-\u9FA5]+$";

        return Pattern.matches(regex,chinese);

    }

    @H_301_8@/** @H_301_8@ * 098 @H_301_8@ * 验证日期(年月日) @H_301_8@ * 099 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@birthday @H_301_8@日期,格式:1992-09-03,或1992.09.03 @H_301_8@ * 100 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 101 @H_301_8@ */ @H_301_8@ public static boolean checkBirthday(String birthday) {
        String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
        return Pattern.matches(regex,birthday);
    }

    @H_301_8@/** @H_301_8@ * 108 @H_301_8@ * 验证URL地址 @H_301_8@ * 109 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@url @H_301_8@格式:[url]http://blog.csdn.net:80/xyang81/article/details/7705960?[/url] 或 [url]http://www.csdn.net:80[/url] @H_301_8@ * 110 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 111 @H_301_8@ */ @H_301_8@ public static boolean checkURL(String url) {

        String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";

        return Pattern.matches(regex,url);

    }

    @H_301_8@/** @H_301_8@ * 118 @H_301_8@ * * 获取网址 URL 的一级域名 @H_301_8@ * 119 @H_301_8@ * [url]http://detail.tmall.com/item.htm?spm=a230r.1.10.44.1xpDSH&id=15453106243&_u=f4ve1uq1092[/url] ->> tmall.com @H_301_8@ * 120 @H_301_8@ * @H_301_8@<p> @H_301_8@ @H_301_8@* 121 @H_301_8@ * @H_301_8@<p> @H_301_8@ @H_301_8@* 122 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@url @H_301_8@123 @H_301_8@ * @H_301_8@@return @H_301_8@124 @H_301_8@ */ @H_301_8@ public static String getDomain(String url) {

        Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",Pattern.@H_301_8@CASE_INSENSITIVE);
        // 获取完整的域名
        // Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)",Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(url);
        matcher.find();
        return matcher.group();
    }

    @H_301_8@/** @H_301_8@ * 134 @H_301_8@ * 匹配中国邮政编码 @H_301_8@ * 135 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@postcode @H_301_8@邮政编码 @H_301_8@ * 136 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 137 @H_301_8@ */ @H_301_8@ public static boolean checkPostcode(String postcode) {

        String regex = "[1-9]\\d{5}";

        return Pattern.matches(regex,postcode);

    }

    @H_301_8@/** @H_301_8@ * 144 @H_301_8@ * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小) @H_301_8@ * 145 @H_301_8@ * @H_301_8@ * @H_301_8@@param @H_301_8@ipAddress @H_301_8@IPv4标准地址 @H_301_8@ * 146 @H_301_8@ * @H_301_8@@return @H_301_8@验证成功返回true,验证失败返回false @H_301_8@ * 147 @H_301_8@ */ @H_301_8@ public static boolean checkIpAddress(String ipAddress) {

        String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
        return Pattern.matches(regex,ipAddress);
    }

    //是否包含 . 号
    public static boolean checkContainsDot(String username) {
        return username.contains(".");
    }

    //是否包含连词符
    public static boolean checkContainsHyphen(String username) {

        return username.contains("-");

    }
    //密码长度 6-20

    public static boolean checkUserPasswordLength(String pwd) {

        return pwd.length() > 5 && pwd.length() < 21;
    }

    public static boolean isValidUserName(String un)

    {
        String regex = "([A-Z0-9a-z-]|[\\u4e00-\\u9fa5])+";
        return Pattern.matches(regex,un);
}
}

猜你在找的正则表达式相关文章