MJAnderson

I would like to write a regular expression that will enforce a rule that the character string cannot contain any repeated characters in sequence.

Example:

abcd - is valid.

ababab - is valid.

abaab - is NOT valid.

I would like to apply this to both alpha characters and non-alpha characters, so:

abc11def - is NOT valid.

I would like to apply this to the following regular expression :

(from http://msdn2.microsoft.com/en-us/library/ms998267.aspx)

( !^[0-9]*$)( !^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$

Do regular expressions allow me to do this, or do I need to write custom code Can anyone show me what needs to be added to the regular expression above to enforce this rule



Re: Regular Expressions How to detect a repeated pattern

OmegaMan

Here is a regex which handles all of your test scenarios. If it returns any match, then there are duplicates. In the example below it cathes the 1's and fs. The trick is to use the backreference \1 which says use the preceding match as a target.

Input Textabc11deff
Regular Expression

([A-Z]|[a-z]|[\d]|[\W])\1

Group CapturesGroups: (0) (1)