^([a-zA-Z0-9!@#$%^&*|()_\-+=\[\]{}:;\"',<.>?\/~`]{4,})$
Would this regular expression work for these rules?
- Must be atleast 4 characters
- Characters can be a mix of alphabet (capitalized/non-capitalized), numeric, and the following characters: ! @ # $ % ^ & * ( ) _ – + = | [ { } ] ; : ‘ ” , < . > ? /
It’s intended to be a password validator. The language is PHP.
Yes?
Honestly, what are you asking for? Why don’t you test it?
If, however, you want suggestions on improving it, some questions:
/\w/instead of/0-9a-zA-Z_/?()s? You don’t need to capture the whole thing, since you already have the whole thing, and they aren’t needed to group anything.What I would do is check the length separately, and then check against a regex to see if it has any bad characters. Your list of good characters seems to be sufficiently large that it might just be easier to do it that way. But it may depend on what you’re doing it for.
EDIT: Now that I know this is PHP-centric,
/\w/is safe because PHP uses the PCRE library, which is not exactly Perl, and in PCRE,\wwill not match Unicode word characters. Thus, why not check for length and ensure there are no invalid characters:Alternatively, use the little-used POSIX character class
[[:graph:]]. It should work pretty much the same in PHP as it does in Perl.[[:graph:]]matches any alphanumeric or punctuation character, which sounds like what you want, and[[:^graph:]]should match the opposite. To test if all characters match graph:To test if any characters don’t match graph: