Checking for consecutive char's

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Checking for consecutive char's

Post by Jenk »

A friend has asked me if I know of a pattern to detect repeated characters in a string, he is using it for 'password authentication' where he has the rules : 8-12 chars, alphanum only, first char must be alpha', and password must not contain consecutive char's more than twice.

Namely he wants to stop people using 'aaaaaaa' as their password etc.

So, the others we have sorted, but the consecutive char's is posing problems. Can regex detect consecutive characters, and importantly, can we specify the limit?

After some thought 'No' is the answer, but posting for a 'second opinion' given I am not a RegEx god :p

and for the sake of it, this is what we have come up with for the other rules (but have not tested):

Code: Select all

^([a-zA-Z]+?[a-zA-Z0-9]+){8,12}$
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

This will check for 8 non-repeated chars:

Code: Select all

^((\w)(?!\2)){8}$
Post Reply