[英]Regex without special characters
我正在使用正則表達式來驗證密碼
這是我的正則表達式配置
@"^(?!.*[\s])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$"
我的標准是
我正在等待的測試:
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);
}
}
嘗試將此 Regex 放入您的代碼中,它將適用於您的條件。
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,32}$/
用下面的代碼替換模式變量:)
string pattern = @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,32}$";
@Arpit Patel 的回答非常好,但我會花一點時間來解釋你的方法有什么問題。 讓我們分解一下:
所以 Arpit 的回答是:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,32}$
取消“任何事情”並插入合法字符。 因此,由於禁止使用空格,因此不再需要負面展望。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.