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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • css改变鼠标样式

    css改变鼠标样式CSS控制鼠标通过cursor属性来实现,该属性可以在任何标记中使用,因此,可以改变各种页面元素的鼠标效果。//设置为小手cursor:pointer//设置为左右箭头cursor:w-resize或cursor:e-resize//设置为斜箭头cursor:nw-resize或cursor:ne-resize//设置为全方位箭头cursor:move此外,cursor还有很多鼠标指针效果,如下图:浏览器调用的是操作系统的鼠标效果,但是,不同的操作系统之间还是存在

    2022年5月31日
    73
  • RBF(径向基)神经网络

    RBF(径向基)神经网络

    2021年11月20日
    43
  • pytest指定用例_pytest测试框架从入门到精通

    pytest指定用例_pytest测试框架从入门到精通前言测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果.有些同学在写用例的时候,用例写了先后顺序,有先后顺序后,后面还会有新的问题(如:上个用例返回

    2022年7月30日
    11
  • pyinstall 使用「建议收藏」

    pyinstall 使用「建议收藏」https://blog.csdn.net/HW140701/article/details/93494869

    2022年10月23日
    0
  • window route del 删除默认路由_route命令用法

    window route del 删除默认路由_route命令用法Windows系统route路由增加改变删除routeaddroutedeleteroutechange

    2022年8月12日
    5
  • MySql数据库备份与恢复——使用mysqldump 导入与导出方法总结

    MySql数据库备份与恢复——使用mysqldump 导入与导出方法总结MySql数据库备份与恢复——使用mysqldump导入与导出方法总结mysqldump客户端可用来转储数据库或搜集数据库进行备份或将数据转移到另一个sql服务器(不一定是一个mysql服务器)。转储包含创建表和/或装载表的sql语句。ps、如果在服务器上进行备份,并且表均为myisam表,应考虑使用mysqlhotcopy,因为可以更快地进行备份和恢复。本文从三部分介绍了mys…

    2022年6月13日
    72

发表回复

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

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