常规的前pression密码 [英] regular expression for password
问题描述
请能有人给我密码定期EX pression以下规则。
Please can some one give me regular expression for password with the following rules.
密码应至少为7个字符长。
它应该包含至少3个数字和一个英文字母。
密码可以接受的数字,字母,特殊字符除了数字任何次数应为最少3
Password should be at least 7 characters long. It should contain minimum 3 digits and one alphabetic character. Password can accept numbers, alphabets, special characters any number of times except numbers should be minimum 3.
推荐答案
正常前pressions不确保字符的特定组特别好的出现一定的次数。虽然它可能是可能的 - 它无疑会被进行卷积的和非显而易见
Regular expressions aren't particularly good at ensuring that particular groups of characters appear a certain number of times. While it's probably possible - it would no doubt be convoluted and non-obvious.
如果您是在.NET(C#或VB),可以使用一个简单的验证功能类似编程:
If you're programming in .NET (C# or VB) you can use a simple validation function something like:
bool ValidatePasswordCompliance( string password )
{
int countDigits = 0;
int countAlpha = 0;
int countOthers = 0;
foreach( char c in password )
{
countDigit += c.IsDigit ? 1 : 0;
countAlpha += c.IsAlpha ? 1 : 0;
countOther += !(c.IsAlpha || c.IsDigit) ? 1 : 0;
}
return countDigits >= 3 && (countDigits + countAlpha + countOthers) >= 7;
}
如果你正在使用.NET 3.5或更高的工作,你可以使用LINQ来简化:
If you're working with .NET 3.5 or higher, you could use LINQ to simplify this:
bool ValidatePasswordCompliance( string password )
{
return password.Count() >= 7 &&
password.Count( x => x.IsDigit ) >= 3;
}
这篇关于常规的前pression密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!