Yes?
Honestly, what are you asking for? Why don't you test it?
If, however, you want suggestions on improving it, some questions:
- What is this regex checking for?
- Why do you have such a large set of allowed characters?
- Why don't you use
/\w/
instead of /0-9a-zA-Z_/
?
- Why do you have the whole thing in
()
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, \w
will not match Unicode word characters. Thus, why not check for length and ensure there are no invalid characters:
if(strlen($string) >= 4 && preg_match('[\s~\\]', $string) == 0) {
# valid password
}
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:
preg('^[[:graph:]]+$', $string) == 1
To test if any characters don't match graph:
preg('[[:^graph:]]', $string) == 0