加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:29:14 所属栏目:百科 来源:网络整理
导读:需要一个正则表达式来验证用户名: 应该允许尾随空格但不允许字符之间的空格 必须至少包含一个字母,可能包含字母和数字 最多7-15个字符(字母数字) 不能包含特殊字符 允许下划线 不知道该怎么做.任何帮助表示赞赏.谢谢. 这是我使用的,但它允许字符之间的空格
需要一个正则表达式来验证用户名:

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

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

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

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

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

试试这个:
foundMatch = Regex.IsMatch(subjectString,@"^(?=.*[a-z])w{7,15}s*$",RegexOptions.IgnoreCase);

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

所以基本上我使用三个规则.一个检查是否至少存在一个字母.
另一个检查字符串是否只包含alphas和_,最后我接受尾随空格,至少7个最多15个alpha.你的情况很好.坚持下去,你也会在这里回答问题:)

分解:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读