正则表达式在软件开发校验数据格式是否符合要求时,使用相当广泛
//校验密码强度
function CheckPassWordStrong(const APassword: string): Boolean;
const
// 这里定义密码的强度规则,例如:至少8个字符,包含大写字母、小写字母、数字、特殊字符
StrongPasswordRegex = '^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$';
begin
Result := TRegEx.IsMatch(APassword, StrongPasswordRegex);
end;
//校验电子邮箱地址
function CheckEmailAddress(const EmailAddr: string): Boolean;//验证Email
const
EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' + { }
'(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' + { }
'{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$';
begin
try
Result := TRegEx.IsMatch(EmailAddr, EMAIL_REGEX);
finally
end;
end;
//校验电话或手机
function CheckTelePhoneOrMobile(const ANumber: string): Boolean;//验证手机号或座机号
const
PHONE_REGEX = '^(0\d{2,3}-\d{7,8})|(1[3-9]\d{9})$';
begin
try
Result := TRegEx.IsMatch(ANumber, PHONE_REGEX);
finally
end;
end;
//检验MAC地址
function CheckMACAddress(const AValue: string): Boolean;
const
_REGEX = '^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$';
begin
try
Result := TRegEx.IsMatch(AValue, _REGEX);
finally
end;
end;
Pascal