I would like to detect the following sequences:
a
aA
aAa
aAaA
...
where a~[a-z] and A~[A-Z], the case alternates and the first letter is always lower-case.
Thanks,
Tom
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
The period (.) represents the wildcard character. Any character (except for the newline character) will be matched by a period in a regular expression; when you literally want a period in a regular expression you need to precede it with a backslash.
[a-z]([A-Z][a-z])*[A-Z]?
The regex that @tanascius gave is fine, and based on that, a shorter one could be:
([a-z][A-Z])*[a-z]?
A major difference is that this one will match the empty string. I wasn't sure from the examples if that was allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With