小编Nat*_*han的帖子

创建一个RegEx以验证用户名

我编写此代码来验证用户名是否符合给定条件,有没有人看到我如何将2个RegEx合并为一个?代码是c#

    /// <summary>
    /// Determines whether the username meets conditions.
    /// Username conditions:
    /// Must be 1 to 24 character in length
    /// Must start with letter a-zA-Z
    /// May contain letters, numbers or '.','-' or '_'
    /// Must not end in '.','-','._' or '-_' 
    /// </summary>
    /// <param name="userName">proposed username</param>
    /// <returns>True if the username is valid</returns>
    private static Regex sUserNameAllowedRegEx = new Regex(@"^[a-zA-Z]{1}[a-zA-Z0-9\._\-]{0,23}[^.-]$", RegexOptions.Compiled);
    private static Regex sUserNameIllegalEndingRegEx = new Regex(@"(\.|\-|\._|\-_)$", RegexOptions.Compiled);
    public static bool IsUserNameAllowed(string userName)
    { …
Run Code Online (Sandbox Code Playgroud)

c# regex

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

regex ×1