我正在我的新项目中进行一些密码验证。我有这个规则,我想使用 php preg_match() 函数转换正则表达式模式:
提前谢谢您!
试试这个
(?s)^(\S{4,20})$
解释
"(?s)" + // Match the remainder of the regex with the options: dot matches newline (s)
"^" + // Assert position at the beginning of the string
"(" + // Match the regular expression below and capture its match into backreference number 1
"\\S" + // Match a single character that is a “non-whitespace character”
"{4,20}" + // Between 4 and 20 times, as many times as possible, giving back as needed (greedy)
")" +
"$" // Assert position at the end of the string (or before the line break at the end of the string, if any)
这会起作用。此声明中的唯一要求是至少 4 个、最多 20 个“允许”的字符。
/^[a-zA-Z0-9\W][\S]{4,20}$/
如果您希望每种类型至少需要一种:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[0-9A-Za-z\W][^\s]{4,20}$/
第二个选项解释:
//Look ahead for a lower case
(?=.*[a-z])
//Look ahead for an uppercase
(?=.*[A-Z])
//Look ahead for a number
(?=.*\d)
//Look ahead for a non-word character
(?=.*\W)
//Specify allowed characters (not space), minimum of 4, and maximum of 20 chars
[0-9A-Za-z\W][^\s]{4,20}