我编写此代码来验证用户名是否符合给定条件,有没有人看到我如何将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)
{
if (string.IsNullOrEmpty(userName)
|| !sUserNameAllowedRegEx.IsMatch(userName)
|| sUserNameIllegalEndingRegEx.IsMatch(userName)
|| ProfanityFilter.IsOffensive(userName))
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果我正确理解您的要求,下面应该是您想要的.本\w场比赛字母,数字,或_.
的负回顾后(的(?<![-.])一部分)允许_除非前面的字符是.或-.
@"^(?=[a-zA-Z])[-\w.]{0,23}([a-zA-Z\d]|(?<![-.])_)$"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10781 次 |
| 最近记录: |