Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex To Validate A Letter Range With Repeats

Tags:

regex

I would like to validate a string for letters A-Z, AA-ZZ, AAA-ZZZ, and so on with a regex. I know [A-Z] will validate the first case but what about the others.

A, B, C, D, E, .... Z

or

AA, BB, CC, DD, EE, .... ZZ

or

AAA, BBB, CCC, DDD, EEE, ... ZZZ

and so on...

like image 766
Mike Flynn Avatar asked Nov 17 '25 14:11

Mike Flynn


2 Answers

This is a pain to write, but if you want to match any number of letters and require them to all be the same letter, you could use something like this:

^(A+|B+|C+|D+ ...)$

and so on through the rest of the alphabet.

like image 195
Jamey Sharp Avatar answered Nov 19 '25 09:11

Jamey Sharp


Try

^(\w)\1*$

You match any word character. If it is more than one char, it has to be the same.

like image 44
Albrecht Striffler Avatar answered Nov 19 '25 08:11

Albrecht Striffler



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!