Determine if a string is even or odd length with regex

I am unable to create a regular expression with a set of lines over {a, b, c} , the length is odd with exactly one a . Here is my best attempt:

 (bb|bc|cb|cc)*a(bb|bc|cb|cc)* 

This is good for even b and c on either side of a , but does not take into account the odd combination of b and c on either side of a .

Any clues?

+6
regex
source share
1 answer

Your line will be a prefix followed by a followed by a suffix.

Both prefixes and suffixes can be zero length. If not, they should be either even or both uneven. This means that you have two main cases.

 EVENPREFIX a EVENSUFFIX | UNEVENPREFIX a UNEVENSUFFIX 

Try ( incomplete and incorrect ):

 ([bc][bc])*a([bc][bc])*|([bc][bc][bc])*a([bc][bc][bc])* 

There remains one more uneven case: one [bc] :

 (([bc][bc])*a([bc][bc])*)|([bc]([bc][bc])*a[bc]([bc][bc])*) 

According to http://www.fileformat.info/tool/regex.htm , this is consistent

  • a
  • cac
  • ccabb

I expect this to fit the rest too ...

The left side guarantees even (or empty) sequences b or c . The right side is either a single b or c , followed by several of the two (so that it remains uneven).

Kobe came up with this refinement above:

 ([bc][bc])*(a|[bc]a[bc])([bc][bc])* 

How it works?

The first group is guaranteed to be even. The second group is guaranteed to be uneven with one a inside. The third group is guaranteed to be even. Thus, it is guaranteed that everything will be uneven.

+4
source share

All Articles