OK so I’m trying to preg_match a string (an entered password) with a regular expression but after hours of reading/watching regexp info on the internet I still don’t understand it..
I currently have:
‘/^[A-Z]^[a-z]+$/’
The password must:
Be between 8 and 16 characters.
Contain at least 1 Upper case letter
Contain at least 1 Lower case letter
Contain at least 1 number
If you could also include a regular expression with all of the above requirements but also with
Contain at least 1 symbol
for my own learning I’d be grateful.
Please try and explain your Regular Expression so that maybe I can understand it. Thanks!
Between 8 and 16 characters:
At least one uppercase letter
At least one lowercase letter
At least one number
Putting it together:
If you want a Unicode-aware variant (probably futile with PHP, I know):
This works by having a number of positive lookahead assertions at the beginning, each of which checking one desired property of your string. Lookahead assertions check whether the part in parentheses could match at this position, but won’t advance the actual match position in the string. The actual match here just makes sure the length matches your requirement.
Note though, that this should essentially be computationally the same as several regex matches checking for each of the properties. It’s just packaged in a single regex.
Oh, and at least one symbol can be checked likewise with
(?=.*[,.!?])or something similar, depending on what you consider a symbol.