【问题标题】:Regex: to match certain chars but no space allowed正则表达式:匹配某些字符但不允许空格
【发布时间】:2016-07-12 08:02:47
【问题描述】:

我有这个正则表达式:

(.*^(?=.{16,25}$)(?=.*[a-z]{1,})(?=.*[A-Z]{1,})(?=.*[0-9]{1,})(?=.*\W{1,8}).*$)

要匹配密码(字符串):

  • 16-25长度
  • 1 到多个 a-z
  • 1 到多个 A-Z
  • 1 到多个 0-9
  • 1 到多个符号

但我不想要\s,而且我不知道如何将它放入正则表达式中。

有什么建议吗?

我将原始限制字符{1,8} 编辑为 {1,}

【问题讨论】:

  • 您确定该模式完全有效吗?您的意思是需要 1 到 8 个连续小写、大写字母、数字和符号吗?开头的 .* 使长度检查前瞻无效。试试^(?=\S{16,25}$)(?=(?:[^a-z]*[a-z]){1,8})(?=(?:[^A-Z]*[A-Z]){1,8})(?=(?:\D*\d){1,8})(?=(?:\w*\W){1,8})\S*$,它允许在字符串中的任何位置使用 1 到 8 个字符。 \S 应该用于禁止空格。
  • 只说 16-25 个非空白字符而不是任意声明前 1-8 必须是小写,下一个大写不是更简单和(可能)更安全吗,然后是数字,然后是符号?那么你只需要/^[^\s]{16,25}$/ : xkcd.com/936
  • /^[^\s]{16,25}$/ 将允许一个完整的密码 a's
  • @CD001 这不是 OP 的要求 :)
  • 我实际上怀疑 - 我在阅读 RegExp 时有点搞错了......我认为实际需要的要简单得多;长度为 16-25 个字符的字符串,必须在任何位置包含 1-8 个小写、大写数字和符号。这更有意义。

标签: php regex whitespace


【解决方案1】:

说明

^(?=(?:[^a-z]*?[a-z]){1,8}(?!.*?[a-z]))(?=(?:[^0-9]*?[0-9]){1,8}(?!.*?[0-9]))(?=(?:[^A-Z]*?[A-Z]){1,8}(?!.*?[A-Z]))(?=(?:[^!-\/:-@[-`{-~]*?[!-\/:-@[-`{-~]){1,8}(?!.*?[!-\/:-@[-`{-~]))[a-zA-Z0-9!-\/:-@[-`{-~]{16,25}$

** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看

此正则表达式将执行以下操作:

  • 要求字符串长度为 16-25
  • 在字符串中的任何位置都需要 1 到 8 个 a-z 字符,仅此而已
  • 要求字符串中任意位置包含 1 到 8 个 A-Z 字符,仅此而已
  • 在字符串中的任何位置都需要 1 到 8 个 0-9 字符,仅此而已
  • 在字符串的任何位置都需要 1 到 8 个符号,仅此而已
  • 允许零个空格

示例

现场演示

https://regex101.com/r/oS4mY2/2

示例文本

注意:第一个示例来自您的评论,但它包含超过 8 个小写字母,因此不匹配。

aWoeed1#fde39393aii
aWoeed1#fde39393AII

示例匹配

aWoeed1#fde39393AII

说明

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (between 1 and
                             8 times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [^a-z]*?                 any character except: 'a' to 'z' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
    ){1,8}                   end of grouping
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (between 1 and
                             8 times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [^0-9]*?                 any character except: '0' to '9' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
    ){1,8}                   end of grouping
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (between 1 and
                             8 times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [^A-Z]*?                 any character except: 'A' to 'Z' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
    ){1,8}                   end of grouping
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (between 1 and
                             8 times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [^!-\/:-@[-`{-           any character except: '!' to '\/', ':'
      ~]*?                     to '@', '[' to '`', '{' to '~' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [!-\/:-@[-`{-~]          any character of: '!' to '\/', ':' to
                               '@', '[' to '`', '{' to '~'
--------------------------------------------------------------------------------
    ){1,8}                   end of grouping
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [!-\/:-@[-`{-~]          any character of: '!' to '\/', ':' to
                               '@', '[' to '`', '{' to '~'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-9!-\/:-@[-      any character of: 'a' to 'z', 'A' to 'Z',
  `{-~]{16,25}             '0' to '9', '!' to '\/', ':' to '@', '['
                           to '`', '{' to '~' (between 16 and 25
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

  • 哇,太好了,谢谢!我正在为我的 Laravel 框架制作一个助手,用户可以配置我必须使用哪种密码/验证...字符串的长度、0-9a-zA-Z 和符号的最小值和最大值等等正则表达式真是太棒了
【解决方案2】:

说明

在您的表达式中,(?=.*[A-Z]{1,}) 之类的前瞻性正在测试您是否有 1 个或多个 A-Z。如果您有任何数量(大于零)所需的字符类,这些将返回 true。让它继续测试以查看是否有超过 1 个匹配项只是多余的。

^(?=(?:[^a-z]*?[a-z]){1})(?=(?:[^0-9]*?[0-9]){1})(?=(?:[^A-Z]*?[A-Z]){1})(?=(?:[^!-\/:-@[-{-~]*?[!-/:-@[-{-~]){1})[a-zA-Z0-9!-\/:-@[-{-~]{16,25}$`

** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看

此正则表达式将执行以下操作:

  • 要求字符串长度为 16-25
  • 在字符串中的任意位置至少需要 1 个a-z 字符
  • 要求字符串中任意位置至少有 1 个A-Z 字符
  • 要求字符串中任意位置至少有 1 个0-9 字符
  • 在字符串中的任意位置至少需要 1 个符号
  • 允许零个空格

示例

现场演示

https://regex101.com/r/oS4mY2/3

示例文本

注意最后一行不匹配,因为它没有符号

aWoeed1#fde39393aii
aWoeed1#fde39393AII
aaaaaaaaaaaaaaaaa1A

示例匹配

MATCH 1
0.  [0-19]  `aWoeed1#fde39393aii`

MATCH 2
0.  [20-39] `aWoeed1#fde39393AII`

说明

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (1 times):
--------------------------------------------------------------------------------
      [^a-z]*?                 any character except: 'a' to 'z' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
    ){1}                     end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (1 times):
--------------------------------------------------------------------------------
      [^0-9]*?                 any character except: '0' to '9' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
    ){1}                     end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (1 times):
--------------------------------------------------------------------------------
      [^A-Z]*?                 any character except: 'A' to 'Z' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
    ){1}                     end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (1 times):
--------------------------------------------------------------------------------
      [^!-\/:-@[-`{-           any character except: '!' to '\/', ':'
      ~]*?                     to '@', '[' to '`', '{' to '~' (0 or
                               more times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      [!-\/:-@[-`{-~]          any character of: '!' to '\/', ':' to
                               '@', '[' to '`', '{' to '~'
--------------------------------------------------------------------------------
    ){1}                     end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-9!-\/:-@[-      any character of: 'a' to 'z', 'A' to 'Z',
  `{-~]{16,25}             '0' to '9', '!' to '\/', ':' to '@', '['
                           to '`', '{' to '~' (between 16 and 25
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多