c#正则表达式,电话号码,密码字母与数字组合

2023-08-24 14:44:48 浏览数 (3)

1.是否是电话号码

代码语言:javascript复制
public static bool IsPhoneOK(string num)
    {
        if (num.Length < 11)
        {
            return false;
        }
        //电信手机号码正则
        string dianxin = @"^1[3578][01379]d{8}$";
        Regex regexDX = new Regex(dianxin);
        //联通手机号码正则
        string liantong = @"^1[34578][01256]d{8}";
        Regex regexLT = new Regex(liantong);
        //移动手机号码正则
        string yidong = @"^(1[012345678]d{8}|1[345678][012356789]d{8})$";

        string phone = @"^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])d{8}$";
        Regex regexYD = new Regex(yidong);
        Regex phoneRe = new Regex(phone);
        if (regexDX.IsMatch(num) || regexLT.IsMatch(num) || regexYD.IsMatch(num) || phoneRe.IsMatch(num))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

2.密码中为数字和字母组成,至少包含1个大写或者1个小写 例如:123456a

代码语言:javascript复制
Regex reg = new Regex(@"^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{6,10}$");
        return reg.IsMatch(str);

1 人点赞