读书笔记----------第五章----------字符串


题目:使用正则表达式来判断字符串text是否为合法手机号。

代码:

//=========参考答案==========
public class Eval { // 新建类
    public static void main(String[] args) { // 主方法
        String regex = "1[35]\d{9}";  //难点一、[xyz]字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"。
        String text = "15000000000";
        if (text.matches(regex)) {
            System.out.println(text + "是合法的手机号");
        }
    }
}

//==========自编============
package com.xxgpra.CH5;

public class islegitimacy {
    public static void main(String[] args) {
        String mobilenum = new String("\d{11}");
        String num = "text";
        
        if(num.matches(mobilenum)){
            System.out.println("合法");
        }else{
            System.out.println("不合法");
        }
    }
}
   

难点一:

用正则表达式[35],来验证手机后中是否含有3或5

原文地址:https://www.cnblogs.com/developmental-t-xxg/p/10057006.html