Android 正则表达式大全(通用版)

来源:互联网 发布:疯帽子雾化器做丝数据 编辑:程序博客网 时间:2024/06/04 04:48

正则表达式有很多,这里只是一种通用版本;

public class Validator {    /**     * 正则表达式:验证用户名(不包含中文和特殊字符)如果用户名使用手机号码或邮箱 则结合手机号验证和邮箱验证     */    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";    /**     * 正则表达式:验证密码(不包含特殊字符)     */    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";    /**     * 正则表达式:验证手机号     */    public static final String REGEX_MOBILE = "^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";    /**     * 正则表达式:验证邮箱     */    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";    /**     * 正则表达式:验证汉字(1-9个汉字)  {1,9} 自定义区间     */    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5]{1,9}$";    /**     * 正则表达式:验证身份证     */    //    public static final String REGEX_ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)";    //定义判别用户身份证号的正则表达式(要么是15位,要么是18位,最后一位可以为字母)    public static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";    /**     * 正则表达式:验证IP地址     */    public static final String REGEX_IP_ADDR = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";    /**     * 正则表达式:验证长度是否符合     */    public static final String REGEX_LENGTH = "^[a-zA-Z0-9]{6,8}$";    /**     * 校验用户名     *     * @param username     * @return 校验通过返回true,否则返回false     */    public static boolean isUserName(String username) {        return Pattern.matches(REGEX_USERNAME, username);    }    /**     * 校验密码     *     * @param password     * @return 校验通过返回true,否则返回false     */    public static boolean isPassword(String password) {        return Pattern.matches(REGEX_PASSWORD, password);    }    /**     * 校验手机号     *     * @param mobile     * @return 校验通过返回true,否则返回false     */    public static boolean isMobile(String mobile) {        return Pattern.matches(REGEX_MOBILE, mobile);    }    /**     * 校验邮箱     *     * @param email     * @return 校验通过返回true,否则返回false     */    public static boolean isEmail(String email) {        return Pattern.matches(REGEX_EMAIL, email);    }    /**     * 校验汉字     *     * @param chinese     * @return 校验通过返回true,否则返回false     */    public static boolean isChinese(String chinese) {        return Pattern.matches(REGEX_CHINESE, chinese);    }    /**     * 校验身份证     *     * @param idCard     * @return 校验通过返回true,否则返回false     */    public static boolean isIDCard(String idCard) {        return Pattern.matches(REGEX_ID_CARD, idCard);    }    /**     * 校验IP地址     *     * @param ipAddress     * @return     */    public static boolean isIPAddress(String ipAddress) {        return Pattern.matches(REGEX_IP_ADDR, ipAddress);    }    /**     * 检验长度6-8     *     * @param length     * @return     */    public static boolean isLength(String length) {        return Pattern.matches(REGEX_LENGTH, length);    }}

举个例子吧:


    if (Validator.isIDCard(identityCard)) {          } else {           }

有其他漏掉的请评论回复下……



原创粉丝点击