tags:

views:

67

answers:

3

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

+12  A: 
[a-z]([A-Z][a-z])*[A-Z]?
tanascius
A: 

Tanascius example would match things like 'Aa' and 'aAA'. I haven't tested this, but should work mostly:

^([a-z][A-Z]?)+$
gnp
This will also allow `aa`.
Gumbo
His example won't. Your example on the other hand will match stuff like `aaaBaB`.
Blixt
How should my regex match Aa? I demand a lower case character in the first position ...
tanascius
This regex is just incorrect---it matches an arbitrary sequence of lowercase characters at any time, which is exactly contradicted by the specification. Tanascius has the right idea.
Antal S-Z
+4  A: 

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.

JXG
+1 too: If a zero-length string is valid, this would be the way to go. But I think @tanascius is right, and at least one character is required.
Alan Moore
Nice! Thank you.
Tom