跟在PHP regular expression to match alpha-numeric strings with some (but not all) punctuation之后,我需要接受至少两种类型的字符,它们必须是数字,字母或标点符号中的一种,字符串必须介于6和18个字符。这是最好的方式吗?我使用RegExLib中的模式构造了这个正则表达式。PHP正则表达式:字符串必须包含字符类型
preg_match('@' .
// one number and one letter
'^(?=.*\d)(?=.*[a-zA-Z])[!-%\'-?A-~]{6,18}$' .
// one number and one punctuation
'|^(?=.*\d)(?=.*[!-%\'-/:-?\[-`{-~])[!-%\'-?A-~]{6,18}$' .
// or one punctation and one
'|^(?=.*[!-%\'-/:-?\[-`{-~])(?=.*[a-zA-Z])[!-%\'-?A-~]{6,18}$' .
'@i', $pass, $matches);
我测试了我的正则表达式所有三种类型,它仍然工作。前视也负责排序。但我看到你的更可读性和工作。 – nymo