0


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 Answers2

1

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,}.

Jerry
  • 70,495
  • 13
  • 100
  • 144
0

Why not just:

preg_match('/(.)\1\1/', $p)
Toto
  • 89,455
  • 62
  • 89
  • 125