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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题