Reputation:
I am new in regular expression and I was doing some form validation using regular expression. But the problem is most of the regular expression are like:
^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%#a-zA-Z\d]+$
This one I am using for password validation. For other form validation I found lot of such expression here. Now the problem is when I use them in my code as follows
if (preg_match('^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%@#a-zA-Z\d]+$', $password))
I get at least one error. Most of the time it show erro No ending delimiter or unknown modifier etc.
Upvotes: 1
Views: 155
Reputation: 32232
Direct answer: You have no delimiters on your expression. PCRE grabs the first character ^
assumes it's the delimiter, and throws the error because it doesn't find a closing ^
at the end of the regex.
Indirect answer: Like Andy-Lester commented, your regex is over-complex and pretty much unreadable to anyone that isn't a regex guru. I use the following which is more readable and more maintainable.
$req_regex = array(
'/[A-Z]/', //uppercase
'/[a-z]/', //lowercase
'/[^A-Za-z]/' //non-alpha
);
foreach($req_regex as $regex) {
if( !preg_match($regex, $password) ) {
return NULL;
}
}
Upvotes: 1
Reputation: 59997
The problem with the expression you have given is that you do not have the delimiters around the expression.
For complex regular expressions it is best to build them up piecemeal. I have found the add-on for Firefox (https://addons.mozilla.org/en-us/firefox/addon/rext/) useful.
Upvotes: 0
Reputation: 71384
You don't have a delimiter around your expression.
Try this:
$pattern = '/^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%#a-zA-Z\d]+$/';
preg_match ($pattern, $password);
Upvotes: 2