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


相关推荐

  • 监控android USB拔插

    监控android USB拔插最近在做项目中遇到一个需要监控USB拔插来关闭服务的问题,当时查了不少资料,都是说android3.0以上的USB类可以监控,╮(╯▽╰)╭比较难搞后来发现其实可以变通的监控外部电源来实现,相当于监控USB了呵呵,记录下来Intent.ACTION_POWER_DISCONNECTED就是它了

    2022年5月7日
    39
  • Struts2 漏洞集合

    Struts2 漏洞集合Struts2漏洞集合总结了一部分Strtus2漏洞,虽然现在这部分的漏洞很少了,但也是学习的一部分,收集的并不全面,后续会做补充。漏洞环境搭建可以使用在线的 Vulfocus ,或者使用docker部署S2-001(CVE-2007-4556)该漏洞因为用户提交表单数据并且验证失败时,后端会将用户之前提交的参数值使用OGNL表达式%{value}进行解析,然后重新填充到对应的表单数据中。例如注册或登录页面,提交失败后端一般会默认返回之前提交的数据,由于后端使用

    2022年7月19日
    17
  • 用xerces-c来进行xml schema校验「建议收藏」

    用xerces-c来进行xml schema校验

    2022年1月25日
    43
  • python进阶(22)pydantic–数据类型校验[通俗易懂]

    python进阶(22)pydantic–数据类型校验[通俗易懂]pydantic库的作用pydantic库是一种常用的用于数据接口schema定义与检查的库。Pydantic在运行时强制执行类型提示,并在数据无效时提供用户友好的错误信息。pydantic安

    2022年7月31日
    7
  • 连接共享打印机显示0x000bcb_0x00004005打印机连接

    连接共享打印机显示0x000bcb_0x00004005打印机连接以win7为例,在局域网中,安装共享打印机时,会出现安装失败,打开详细信息显示为0x000000bcb,重新安装也不行,具体操作如下:连接共享打印机出现0x000000bcb问题的解决方法1打开控制面板,点击卸载程序,如下:2点击”查看已安装的更新“如下:3点击右上角“搜索已安装更新”,输入:”KB4022722“,点击搜索,会在下面的找到”KB4022722“的更新,点击卸载就…

    2022年10月7日
    3
  • android中app的更新案例

    android中app的更新案例http://blog.csdn.net/android_tutor/article/details/7015986

    2022年6月25日
    27

发表回复

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

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