gpt4 book ai didi

c# - 没有特殊字符的正则表达式

转载 作者:行者123 更新时间:2023-12-04 08:16:09 24 4
gpt4 key购买 nike

我正在使用正则表达式来验证密码
这是我的正则表达式配置

@"^(?!.*[\s])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$"
我的标准是
  • 密码必须至少包含一个大写字母、一个小写字母和一个数字;
  • 不能有任何特殊字符、重音或空格;
  • 此外,密码的长度可以是 6 到 32 个字符。

  • 我正在等待的测试:
  • BIT123456 无效
  • Year2021 有效
  • Fing!2020 Is Invalid ( but it returns valid )

  • 我检查错误的代码:
    using System;
    using System.Text.RegularExpressions;

    class Program
    {

    static void Main(string[] args)
    {

    var totalTestCases = int.Parse(Console.ReadLine());

    string S = "";
    string pattern = @"^(?!.*[\s])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$";

    var n = 0;
    do
    {
    n++;
    S = Console.ReadLine();
    if (S != "")
    {
    // Console.WriteLine("{0}", Regex.IsMatch(S, pattern) ? "Senha valida." : "Senha invalida.");
    string test = Regex.IsMatch(S, pattern) ? "Password valid." : "Password invalid.";
    Console.WriteLine(test);
    }
    } while (n < totalTestCases);
    }
    }

    最佳答案

    尝试将此正则表达式放入您的代码中,它将适用于您的条件。

    /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,32}$/
    用下面的代码替换模式变量:)
    string pattern = @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,32}$";

    关于c# - 没有特殊字符的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65696059/

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