tags:

views:

54

answers:

2

Im trying to construct a regular expression to just match if the length of the string is odd. I have not had any success so far.

2313432 - true

12 - false

121111111111111 - true

Thanks

+4  A: 

How about something like: ^(..)*.$ ?

Jerry Coffin
@Dacto: I was going to say the same thing, but he had made an edit to his answer. Refresh and you'll see that this is now correct EXCEPT that it will not match strings of length 1. (I have no idea why people are upvoting it, now or before the edit!)
Platinum Azure
`'^((..)+.|.)$'` adds a match for a length 1 string (I'd still use `strlen()` though)
Seth
@Seth: That regex is more complicated than it needs to be. (See my answer)
Platinum Azure
There we go. (-1 removed)
Platinum Azure
@Platinum - yes, it is.
Seth
+6  A: 

You want this regular expression:

^.(..)*$

This says, match one character, then zero or more sets of two characters, all of which is anchored to the start and end of the string.

Platinum Azure