$re = '/^(?=.*[\d])(?=.*[a-z])(?=.*[A-Z])(?=.*([^\w\s]|[_]))(\S+)$/m';
$str = '123456789
123ABCDEF
123ABDdef
123ABCdeF$
123ABCdeF_
123ABCdeF$ 1234
123ABCdeF$ 1ABC
123ABCdeF$ 1Abc
123ABCdeF$ 1Ab$
123acC&
123acC &
test@123
test_@123A
test_@123A sdf
_123456789
_123ABCDEF
_123ABDdef
_123ABD def
1Ab2
1Ab!
!123
!ABC
!1Ab
123_
1AB_
1Ab_
';
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