Regex:php的密码验证

Regex:php的密码验证,php,regex,Php,Regex,我正在对我的新项目进行密码验证。我想使用php preg_match()函数在正则表达式模式上转换以下规则: 接受所有字符 除了空格 至少4个字符 最多20个字符 提前谢谢你 试试这个 (?s)^(\S{4,20})$ 解释 "(?s)" + // Match the remainder of the regex with the options: dot matches newline (s) "^" + // Assert position at the begi

我正在对我的新项目进行密码验证。我想使用php preg_match()函数在正则表达式模式上转换以下规则:

  • 接受所有字符
  • 除了空格
  • 至少4个字符
  • 最多20个字符

提前谢谢你

试试这个

(?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)

试试这个

(?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)