【发布时间】:2011-03-09 20:52:34
【问题描述】:
跟进PHP regular expression to match alpha-numeric strings with some (but not all) punctuation,对于长度必须在 6 到 18 个字符之间的字符串,我需要接受至少 2 种类型的字符,它们必须是数字、字母或标点符号之一。这是最好的方法吗?我使用来自RegExLib 的模式构建了这个正则表达式。
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);
【问题讨论】: