gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:19:26 27 4
gpt4 key购买 nike

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

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

  • 不知道如何做到这一点。任何帮助表示赞赏。谢谢你。

    这是我使用的,但它允许字符之间有空格
    "(?=.*[a-zA-Z])[a-zA-Z0-9_]{1}[_a-zA-Z0-9\\s]{6,14}"

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

    最佳答案

    尝试这个:

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

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

    所以基本上我使用三个规则。一个检查是否至少存在一个字母。
    另一个检查字符串是否仅包含字母加上 _最后我接受尾随空格和至少 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, as many times as possible, 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, as many times as possible, 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)
    "

    关于regex - 需要正则表达式来验证用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361517/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com