C#使用正则表达式检测特殊字符

C#使用正则表达式检测特殊

发现一篇特别好关于正则表达式的博客写个可以匹配一下各种特殊字符的正则表达式,本人自己也实现了一下,Regex的命名空间为System.Text.RegularExpressions;

    Regex checkUserName = new Regex("^[A-Za-z0-9]+$");//用户名:英文和数字
    Regex checkUserPassword = new Regex("[\u4e00-\u9fa5]");//密码:不带中文
    var resultName = checkUserName.Match(username);
    var resultPassword = checkUserPassword.Match(password);
    if (!resultName.Success)
    {
        Debug.Log("用户名中存在非法字符");
    }
    if (resultPassword.Success)
    {
        Debug.Log("密码中存在非法字符");
    }

你可能感兴趣的:(C#使用正则表达式检测特殊字符)