9

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

Tom
  • 6,991
  • 13
  • 60
  • 78

2 Answers2

17
[a-z]([A-Z][a-z])*[A-Z]?
tanascius
  • 53,078
  • 22
  • 114
  • 136
4

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
  • 7,263
  • 7
  • 32
  • 63
  • 1
    +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 Apr 18 '10 at 09:39