$re = '/^ # start of line
(?=(?:.*[A-Z]){2,}) # 2 upper case letters
(?=(?:.*[a-z]){2,}) # 2 lower case letters
(?=(?:.*\d){2,}) # 2 digits
(?=(?:.*[!@#$%^&*()\-_=+{};:,<.>]){2,}) # 2 special characters
(.{8,}) # length 8 or more
$
/mx';
$str = 'Valid:
ABab12!!
ZZzz11#####
PAssword12!!
PassWord12!!
AA aa 12 ##
A1#a2Z3z5%
Not Valid:
AAaa1234
Password12!!
Abab12!!
ZZzz11#
password12!!
Password12!!
AAAa12##
A1 a2Z3z5%
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php