java如何验证手机号码_Java 手机号码正则表达式验证「建议收藏」

java如何验证手机号码_Java 手机号码正则表达式验证「建议收藏」1.正则表达式的字符串表示StringmobileRegex=”^1(3|4|5|7|8)\\d{9}$”;字符^意义:表示匹配的字符必须在最前边;字符$意义:表示待匹配串的结束位置;字符\d{5,9}意义:\d是数字,{5,9}是5-9位,如果只是验证手机号,且校验的正则表达式如下:/^1[3|5|8][0-9]\d{4,8}$/;不妨写成/^1[3|5|8]\…

大家好,又见面了,我是你们的朋友全栈君。

1.正则表达式的字符串表示 String mobileRegex = “^1(3|4|5|7|8)\\d{9}$”; 字符^意义:表示匹配的字符必须在最前边; 字符$意义:表示待匹配串的结束位置; 字符\d{5,9}意义:\d是数字,{5,9}是5-9位,如果只是验证手机号,且校验的正则表达式如下: /^1[3|5|8][0-9]\d{4,8}$/ ; 不妨写成 /^1[3|5|8]\d{9}$/ ; 2.对手机号字符串的校验 String mobileRegex = “^1(3|4|5|7|8)\\d{9}$”; if (csprop.matches(mobileRegex)){     *******; }else {     *******; } 注: String.java /**      * Tells whether or not this string matches the given regular expression.      *      *

An invocation of this method of the form      * str.matches(regex) yields exactly the      * same result as the expression      *      *

{@link java.util.regex.Pattern}.{@link      * java.util.regex.Pattern#matches(String,CharSequence)      * matches}(
regex
,
str
)

     *      * @param   regex      *          the regular expression to which this string is to be matched      *      * @return 
true if, and only if, this string matches the      *          given regular expression      *      * @throws  PatternSyntaxException      *          if the regular expression’s syntax is invalid      *      * @see java.util.regex.Pattern      *      * @since 1.4      * @spec JSR-51      */     public boolean matches(String regex) {         return Pattern.matches(regex, this);     }      Pattern.java /**      * Compiles the given regular expression and attempts to match the given      * input against it.      *      *

An invocation of this convenience method of the form      *      *

       * Pattern.matches(regex, input);

     *      * behaves in exactly the same way as the expression      *      *

       * Pattern.compile(regex).matcher(input).matches()

     *      *

If a pattern is to be used multiple times, compiling it once and reusing      * it will be more efficient than invoking this method each time. 

     *      * @param  regex      *         The expression to be compiled      *      * @param  input      *         The character sequence to be matched      *      * @throws  PatternSyntaxException      *          If the expression’s syntax is invalid      */     public static boolean matches(String regex, CharSequence input) {         Pattern p = Pattern.compile(regex);         Matcher m = p.matcher(input);         return m.matches();     }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143895.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js获取当前日期时间「建议收藏」

    js获取当前日期时间「建议收藏」functionThistime(){//当前日期vardate=newDate();varyear=date.getFullYear();varmonth=date.getMonth()+1;varday=date.getDate();varmytime=date.toLocaleTimeString();//获取当前时间//myDate.toLocaleString()

    2025年7月11日
    4
  • 在Scala中使用fastJson 解析json字符串

    在Scala中使用fastJson 解析json字符串一 阿里巴巴 FastJson 是一个 Json 处理工具包 包括 序列化 和 反序列化 两部分 它具备如下特征 速度最快 测试表明 fastjson 具有极快的性能 超越任其他的 JavaJsonpars 包括自称最快的 JackJson 功能强大 完全支持 JavaBean 集合 Map 日期 Enum 支持范型 支持自省 无依赖 二 在 Scala 中使用也可使用 fastJson 解析 jso

    2025年9月16日
    0
  • python字典详解_python字典get方法

    python字典详解_python字典get方法字典字典的key和value一一对应的,字典是可变的,也是有序的(python3.6版本开始字典有序),可迭代的增加元素当key不存在时,直接赋值a={"status"

    2022年7月31日
    5
  • Python之json文件

    json简介json是一种轻量级的数据交换格式完全独立于编程语言的文本格式来存储和表示数据简单和清晰的层次结构使得json成为理想的数据交换语言。易于阅读和编写,易于机器解析和生成,并有效地提升

    2021年12月19日
    53
  • java基础-异或运算[通俗易懂]

    java基础-异或运算[通俗易懂]小伙伴们,你们好呀!我是老寇!异或运算主要用于判断两个值是否一样异或运算的3个性质:1.任何数和0进行异或运算,结果是原来的数,即b⊕0=b2.任何数和其自身进行异或运算,结果为0,即b⊕b=0。3.异或运算满足交换律和结合律,即x⊕y⊕x=y⊕(x⊕x)=y例题:力扣刷题题解:classSolution{publicintsingleNumber(int[]nums){intsingle=0;for(intn:num

    2022年10月4日
    3
  • mysql 加入�列,改动列,删除列。

    mysql 加入�列,改动列,删除列。

    2021年12月1日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号