Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky Regular Expression

Tags:

c#

regex

repeat

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?

like image 330
Oisin C. Vera Avatar asked Feb 10 '26 20:02

Oisin C. Vera


1 Answers

^(?!(.)\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.

like image 100
kennytm Avatar answered Feb 13 '26 10:02

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!