.NET Framework Bookmark and Share   
 index > Regular Expressions > How to detect a repeated pattern
 

How to detect a repeated pattern

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?

MJAnderson
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)


Match(1):
0:11
1:1

Match(2):
0:ff
1:f

Regex Info Generated by the Regex Responder V1.0

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)


Match(1):
0:11
1:1

Match(2):
0:ff
1:f

Regex Info Generated by the Regex Responder V1.0

OmegaMan

You can use google to search for other answers

Custom Search

More Threads

• need regular expression to not include strings contains a special charchters
• Match Alpha-Numeric characters
• How to determine the case of substrings with RegExp?
• Help with parsing string data to class properties.
• Regex for bytes?
• Need help in building a validation expression
• regular expression for removing double quote and single quote and any character in a string aspnet 2.0 c#
• string generation using regex (regular expression)
• Accessing Named Groups within Matches
• Search between blocks of text