views:

61

answers:

2

Hi everyone.

I need to allow only alphanumeric characters (with uppercase) from 0-25 chars length and no lazy all-repetition numeric value.

I've got the first part: Regex.IsMatch(tmpResult, "^[0-9A-Z]{0,25}$"); (that's easy)

111112 - match
AABD333434 - match
55555555 - no match
555 - no match

Could anyone please help me with this?

+5  A: 
^(?!(.)\1*$)[0-9A-Z]{0,25}$

The extra (?!(.)\1*$) will reject any strings that is composed of repeating same character.

The (?!…) is a negative lookahead that will cause the primary regex fail if the is matched, and the (.)\1* will match a string of repeating characters.

KennyTM
Works perfect, thanks Kenny.
Oisin C. Vera
A: 

You could just do it using a normal method... Once you have it match your first expression there, just use a subroutine to iterate through each character and return true the first time you encounter a character that differs from the first in the string.

It should return true after checking only the first 2 characters for most strings, unless it's an invalid string.

This should be equally as fast as a regex if not faster, if it is well implemented.

gnomed