Help with a regular expression issue in PHP (I'm probably doing something silly)
I am attempting to integrate a regex that I received from a coworker (who开发者_StackOverflow中文版 implemented it in VB) in a bit of PHP code. The regex is designed to be able to validate whether or not a password meets certain complexity requirements, but my function always returns false, even for a password which does meet our complexity requirements. The requirements are:
a minimum of 8 characters, a maximum of 20 characters, cannot contain a space, must contain at least one (1) Number and one (1) Special Character. The following Special Characters may be used in a Password : - ! @ # $ % & * ( ) _ + = . ?.
The regex I was provided was:
ValidationExpression = "^.*(?=.{8,20})(?=.*\d)(?=.*[a-zA-Z])(?=.*[-!@#$%&*()_+=.?])(?!.*\s).*$"
And the function I wrote to validate a password is:
function validatePassword($pass){
$regex = "^.*(?=.{8,20})(?=.*\d)(?=.*[a-zA-Z])(?=.*[-!@#$%&*()_+=.?])(?!.*\s).*$";
return preg_match($regex, $pass);
}
I suspect that the issue is that I am doing something silly, but as regular expressions still baffle me, I have no idea what the problem is. Any ideas?
You need to enclose the regex in delimiters, ie
$regex = "/^.*(?=.{8,20})(?=.*\d)(?=.*[a-zA-Z])(?=.*[-!@#$%&*()_+=.?])(?!.*\s).*$/";
精彩评论