PHP (Regex) Check if a string contains at least 4 digits + 2 letters -


i check string if contains at least:

2 letters (a-z) case insensitive 4 digits (0-9)

the order not important. a1234b, abcd1234, 4444aa etc etc.

my actual regex

if (preg_match("/[a-z][^a-z]*[a-z]*[0-9]/i",$string)) { echo 'secure'; $continue = true; } 

and doesn't function if string start digit. thank you

^(?=(?:.*[a-za-z]){2})(?=(?:.*[0-9]){4})\w+$ 

you can use lookahead here apply conditions.see demo.

https://regex101.com/r/vv1ww6/22

$re = "/^(?=(?:.*[a-za-z]){2})(?=(?:.*[0-9]){4})\\w+$/m";  $str = "a1234b\nabcd1234\n4444aa\n12\n12a\n12aa22";   preg_match_all($re, $str, $matches); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -