这就是我所拥有的:
Regex.IsMatch(Password.Text, @"^[A-Za-z0-9@#$%^&+=]{3,15}$ ")
它总是返回false。
请帮忙。
答案 0 :(得分:3)
取出正则表达式字符串末尾的空格。另外:纠正了可能的错别字。
Regex.IsMatch(Password.Text, @"^[A-Za-z0-9@#$%^&+=]{3,15}$")
答案 1 :(得分:1)
为什么你想要限制可能的密码?!?
.
和:
?答案 2 :(得分:1)
如上所述,这是一个非常非常糟糕的主意。更好的方法是测试输入密码的密码强度,并设置密码必须打败的分数。
有计算密码强度的算法。以下内容摘自Hagen Reddmann的Delphi加密纲要(因此在Pascal中,但我想这可以很容易地翻译)
function PassphraseQuality(const Password: String): Extended;
// returns computed Quality in range 0.0 to 1.0
// source extracted from Delphi Encryption Compendium, DEC
function Entropy(P: PByteArray; L: Integer): Extended;
var
Freq: Extended;
I: Integer;
Accu: array[Byte] of LongWord;
begin
Result := 0.0;
if L <= 0 then Exit;
FillChar(Accu, SizeOf(Accu), 0);
for I := 0 to L-1 do Inc(Accu[P[I]]);
for I := 0 to 255 do
if Accu[I] <> 0 then
begin
Freq := Accu[I] / L;
Result := Result - Freq * (Ln(Freq) / Ln(2));
end;
end;
function Differency: Extended;
var
S: String;
L,I: Integer;
begin
Result := 0.0;
L := Length(Password);
if L <= 1 then Exit;
SetLength(S, L-1);
for I := 2 to L do
Byte(S[I-1]) := Byte(Password[I-1]) - Byte(Password[I]);
Result := Entropy(Pointer(S), Length(S));
end;
function KeyDiff: Extended;
const
Table = '^1234567890ß´qwertzuiopü+asdfghjklöä#<yxcvbnm,.-°!"§$%&/()=?`QWERTZUIOPÜ*ASDFGHJKLÖÄ''>YXCVBNM;:_';
var
S: String;
L,I,J: Integer;
begin
Result := 0.0;
L := Length(Password);
if L <= 1 then Exit;
S := Password;
UniqueString(S);
for I := 1 to L do
begin
J := Pos(S[I], Table);
if J > 0 then S[I] := Char(J);
end;
for I := 2 to L do
Byte(S[I-1]) := Byte(S[I-1]) - Byte(S[I]);
Result := Entropy(Pointer(S), L-1);
end;
const
GoodLength = 10.0; // good length of Passphrases
var
L: Extended;
begin
Result := Entropy(Pointer(Password), Length(Password));
if Result <> 0 then
begin
Result := Result * (Ln(Length(Password)) / Ln(GoodLength));
L := KeyDiff + Differency;
if L <> 0 then L := L / 64;
Result := Result * L;
if Result < 0 then Result := -Result;
if Result > 1 then Result := 1;
end;
end;