需要正则表达式来验证用户名

107*_*081 5 regex

需要一个正则表达式来验证用户名:

  1. 应该允许尾随空格但不允许字符之间的空格
  2. 必须包含至少一个字母,可能包含字母和数字
  3. 最多7-15个字符(字母数字)
  4. 不能包含特殊字符
  5. 允许下划线

不知道该怎么做.任何帮助表示赞赏.谢谢.

这是我使用的,但它允许字符之间的空格

"(?=.*[a-zA-Z])[a-zA-Z0-9_]{1}[_a-zA-Z0-9\\s]{6,14}"
Run Code Online (Sandbox Code Playgroud)

示例:用户名用户名中不允许使用空格

Fai*_*Dev 4

尝试这个:

\n\n
foundMatch = Regex.IsMatch(subjectString, @"^(?=.*[a-z])\\w{7,15}\\s*$", RegexOptions.IgnoreCase);\n
Run Code Online (Sandbox Code Playgroud)\n\n

也允许使用,_因为您在尝试中允许这样做。

\n\n

所以基本上我使用三个规则。一个用于检查是否至少有一个字母存在。\n另一个用于检查字符串是否仅由字母加 组成_,最后我接受尾随空格和至少 7 个字母,最多 15 个字母。你处于良好的轨道上。坚持下去,你也会在这里回答问题:)

\n\n

分解:

\n\n
    "\n^           # Assert position at the beginning of the string\n(?=         # Assert that the regex below can be matched, starting at this position (positive lookahead)\n   .        # Match any single character that is not a line break character\n      *     # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)\n   [a-z]    # Match a single character in the range between \xe2\x80\x9ca\xe2\x80\x9d and \xe2\x80\x9cz\xe2\x80\x9d\n)\n\\w          # Match a single character that is a \xe2\x80\x9cword character\xe2\x80\x9d (letters, digits, etc.)\n   {7,15}   # Between 7 and 15 times, as many times as possible, giving back as needed (greedy)\n\\s          # Match a single character that is a \xe2\x80\x9cwhitespace character\xe2\x80\x9d (spaces, tabs, line breaks, etc.)\n   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)\n$           # Assert position at the end of the string (or before the line break at the end of the string, if any)\n"\n
Run Code Online (Sandbox Code Playgroud)\n