PHP Regex无连续字符,至少有一个小写和大写,至少一个数字


PHP Regex No Consecutive Characters, at least one lower and uppercase, at least one number?

我一直在尝试制作一个正则表达式,它与具有以下特征的密码相匹配:

  • 至少有一个元音
  • 至少一笔资本
  • 至少一个数字
  • 而且(最重要的)没有连续的字母或数字

无效密码示例

  • ThisIsMyyPassword20-(由于重复"y"answers"s"而失败)
  • IMissUK20-(由于重复"s"而失败)

有效密码示例

  • 这是我的Pasword20
  • IMisUK20
  • IdontKnow20
  • IidontKnow20-(不要失败,因为"I"answers"I"不一样)

BTW:这是我实际使用的正则表达式,但不匹配连续字符。

regex:/^(?=.*[a-z|A-Z]'1{1})(?=.*[A-Z])(?=.*'d).+$/

谢谢大家。

您可以使用此正则表达式满足所有需求:

^(?=.*[A-Z])(?=.*[aAeEiIoOuU])(?=.*'d)(?:([a-zA-Z'd])(?!'1))+$

RegEx演示

这里是正则表达式分解:

^                   # Start
(?=.*[A-Z])         # lookahead to assert a capital letter
(?=.*[aAeEiIoOuU])  # lookahead to assert a vowel
(?=.*'d)            # lookahead to assert a digit
(?:                 # non-capturing group start
   ([a-zA-Z'd])     # match any letter or digit and capture it in group #1
   (?!'1)           # negative lookahead to ensure same char is not repeated
)+                  # non-capturing group end, + ensures 1 ore more of it
$                   # end
^(?=.*[aeiouAEIOU])(?=.*[A-Z])(?=.*'d)(?!.*(.)'1)[a-zA-Z0-9]+$

你可以用这个。请参阅演示。

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