doupa8922 2013-10-04 07:15 采纳率: 100%
浏览 33

Perl Regex防止密码策略重复出现字符


I'm using a passwordpolicy library from this site written in PHP http://craig-russell.co.uk/password-policy/index.html
It uses perl regex for most of its rules. I have been trying to add a new rule to make it not accept 3 repeated characters. I've tried severals regex that has been posted and asked about here and this is the one I've recently tried out [\w((.)\1{3,}]
But it just don't seem to work

It matches aaaa but if you write aaab it still matches it. Seems like it trying to match the string as a whole

This is the array where the regex goes:

$this->rules['max_allowedsame_chars'] = array(
'value' => false,
'type' => 'integer',
'test' => 'return preg_match_all("/[\w ((.)\1{3,})/",$p,$x)<=$v;',
'error' => 'Password cant contain no more than #VALUE# of the same characters');

Any tips would be appreciated.

  • 写回答

2条回答 默认 最新

  • douzong5057 2013-10-04 07:17
    关注

    Don't use square brackets, they mean character class and most metacharacters lose their meaning in them. Try:

    preg_match_all("/(.)\1{2,}/",$p,$x)
    

    This will match if you have 3 repeated characters or more, but fail if there's less.

    The first character is caught in the first capture group and the next two (or more) are matched by \1{2,}.

    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存