Android正则表达式----常用正则积累

前言

有一定开发经验的同学一定都知道正则表达式是什么,也知道它的用处与强大之处,这里就不过多的说了。强大归强大,但是用到的却往往是边边角角的地方,例如电话号码的验证、邮箱的验证等等这些小地方,不过每个应用都是必不可少的,另外正则表达式记忆起来并没有那么容易,用起来还要思考,这种情况我们很自然就会抽取工具类来使用。这篇文章就分享一下我所积累的正则表达式

代码

话不多说,直接上代码,代码不难,大家应该可以看懂的。

public class RegexUtils {

    /**
     * 密码是否正确
     */
    public static final String PASSWORD_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
    /**
     * 匹配全网IP的正则表达式
     */
    public static final String IP_REGEX = "^((?:(?: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))))$";

    /**
     * 匹配手机号码的正则表达式
     * <br>支持130——139、150——153、155——159、170、171、172、176、177、178/180-189号段
     */
    public static final String PHONE_NUMBER_REGEX = "^1{1}(3{1}\\d{1}|5{1}[012356789]{1}|8{1}[0123456789]{1}|7{1}[0123678]{1})\\d{8}$";

    /**
     * 匹配邮箱的正则表达式
     * <br>"www."可省略不写
     */
    public static final String EMAIL_REGEX = "^(www\\.)?\\w+@\\w+(\\.\\w+)+$";

    /**
     * 匹配汉子的正则表达式,个数限制为一个或多个
     */
    public static final String CHINESE_REGEX = "^[\u4e00-\u9f5a]+$";

    /**
     * 匹配正整数的正则表达式,个数限制为一个或多个
     */
    public static final String POSITIVE_INTEGER_REGEX = "^\\d+$";

    /**
     * 匹配身份证号的正则表达式
     */
    public static final String ID_CARD = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";

    /**
     * 匹配邮编的正则表达式
     */
    public static final String ZIP_CODE = "^\\d{6}$";

    /**
     * 匹配URL的正则表达式
     */
    public static final String URL = "^(([hH][tT]{2}[pP][sS]?)|([fF][tT][pP]))\\:\\/\\/[wW]{3}\\.[\\w-]+\\.\\w{2,4}(\\/.*)?$";

    /**
     * 匹配给定的字符串是否是一个邮箱账号,"www."可省略不写
     * @param string 给定的字符串
     * @return true:是
     */
    public static boolean isEmail(String string){
        return string.matches(EMAIL_REGEX);
    }

    /**
     * 匹配给定的字符串是否是一个手机号码,支持130——139、150——153、155——159、180、183、185、186、188、189号段
     * @param string 给定的字符串
     * @return true:是
     */
    public static boolean isMobilePhoneNumber(String string){
        return string.matches(PHONE_NUMBER_REGEX);
    }

    /**
     * 匹配给定的字符串是否是一个全网IP
     * @param string 给定的字符串
     * @return true:是
     */
    public static boolean isIp(String string){
        return string.matches(IP_REGEX);
    }

    /**
     * 匹配给定的字符串是否全部由汉子组成
     * @param string 给定的字符串
     * @return true:是
     */
    public static boolean isChinese(String string){
        return string.matches(CHINESE_REGEX);
    }

    /**
     * 验证给定的字符串是否全部由正整数组成
     * @param string 给定的字符串
     * @return true:是
     */
    public static boolean isPositiveInteger(String string){
        return string.matches(POSITIVE_INTEGER_REGEX);
    }

    /**
     * 验证给定的字符串是否是身份证号
     * <br>
     * <br>身份证15位编码规则:dddddd yymmdd xx p
     * <br>dddddd:6位地区编码
     * <br>yymmdd:出生年(两位年)月日,如:910215
     * <br>xx:顺序编码,系统产生,无法确定
     * <br>p:性别,奇数为男,偶数为女
     * <br>
     * <br>
     * <br>身份证18位编码规则:dddddd yyyymmdd xxx y
     * <br>dddddd:6位地区编码
     * <br>yyyymmdd:出生年(四位年)月日,如:19910215
     * <br>xxx:顺序编码,系统产生,无法确定,奇数为男,偶数为女
     * <br>y:校验码,该位数值可通过前17位计算获得
     * <br>前17位号码加权因子为 Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]
     * <br>验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
     * <br>如果验证码恰好是10,为了保证身份证是十八位,那么第十八位将用X来代替 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )
     * <br>i为身份证号码1...17 位; Y_P为校验码Y所在校验码数组位置
     * @param string
     * @return
     */
    public static boolean isIdCard(String string){
        return string.matches(ID_CARD);
    }

    /**
     * 验证给定的字符串是否是邮编
     * @param string
     * @return
     */
    public static boolean isZipCode(String string){
        return string.matches(ZIP_CODE);
    }

    /**
     * 验证给定的字符串是否是URL,仅支持http、https、ftp
     * @param string
     * @return
     */
    public static boolean isURL(String string){
        return string.matches(URL);
    }


    public static boolean isPassword(String string){
        return string.matches(PASSWORD_REGEX);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,977评论 25 708
  • 推荐几个正则表达式编辑器 Debuggex :https://www.debuggex.com/ PyRegex:...
    木易林1阅读 11,534评论 9 151
  • 初衷:看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印...
    DCbryant阅读 4,059评论 0 20
  • 若想拥有货真价实的更好的人生,必须靠自己脱离底层的自暴自弃或忧虑恐慌,视野也是需要靠自己努力拓宽的,一个人只有走在...
    嬉笑怒骂皆生活阅读 171评论 0 0
  • 多年后的我60岁的那天早晨5点,我起床,去公园晨练,回来后煮了早餐,送完孙子上学,刚好8点。 来到地铁站,人很多,...
    夏风工作室阅读 307评论 0 0