這項工作?
string characters = " -/:[email protected][-`{-~À-ÿ";
string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string[] inputs = {
"AABBCCDD",
"aaaaaaaa",
"11111111",
"a1a1a1a1",
"[email protected]@@@AA",
"A1C EKFE",
"AADE F"
};
foreach (string input in inputs)
{
var counts = input.Cast<char>().Select(x => new { ch = characters.Contains(x.ToString()) ? 1 : 0, letter = letters.Contains(x.ToString()) ? 1 : 0, notmatch = (characters + letters).Contains(x) ? 0 : 1}).ToArray();
Boolean isMatch = (input.Length >= 8) && (input.Length <= 30) && (counts.Sum(x => x.notmatch) == 0) && (counts.Sum(x => x.ch) <= 3);
Console.WriteLine("Input : '{0}', Matches : '{1}'", input, isMatch ? "Match" : "No Match");
}
Console.ReadLine();
我的提示是讓用戶儘可能多地使用他想要的特殊字符 –
爲什麼你想要正則表達式?在計算字符數方面,這並不好。當然,你可以做到這一點,但這是一種痛苦,很難理解或維護。只需使用一些'IndexOf'或'String.Contains',你就完成了。 – HimBromBeere
@RomanoZumbé沒有選擇。 – Szadek35