Losmi
Losmi

Reputation: 41

Regex for Alphabet characters in order (without skipping letters)

I have trouble writing the correct regular expression for following conditions:

-Word should contain letter from alphabet, for example from A to E

-Letters should be in alphabetic order, but without skipping between two letters! , for example: ABCD is a correct word, ACD is not because B is missing

-It can begin with whatever letter from the alphabet, for example: BCD is valid, as well as DE, but again BCE is not because D is missing

-No letter repeating, for example: AAB is not valid, DEE is not valid

I've tried with following logic : ^A?B?C?D?E?$

But with this I can skip between letters which is not allowed. What can I try to make between letters not skippable?

Upvotes: 0

Views: 154

Answers (1)

Guillaume D
Guillaume D

Reputation: 2346

There is no easy way to do this, regex does not support (x+1) type of computation. However, you could do this ugly thing:

^(?:A|AB|ABC|ABCD|ABCDE|B|BC|BCD|BCDE|C|CD|CDE|D|DE|E)$

Upvotes: 1

Related Questions