正则表达式:匹配某些字符但不允许空格

时间:2016-07-12 08:02:48

标签: php regex whitespace

我有这个正则表达式:

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

匹配密码(字符串):

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

但我不想\s,我也不知道如何把它放在正则表达式中。

有什么建议吗?

  

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

2 个答案:

答案 0 :(得分:2)

描述

^(?=(?:[^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}$

Regular expression visualization

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

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

  • 要求字符串长度为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

答案 1 :(得分:0)

描述

在您的表达式中,(?=.*[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} $`

Regular expression visualization

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

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

  • 要求字符串长度为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