Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if string is even or odd length with regular expression

Tags:

regex

I am having trouble building a regular expression with the set of strings over {a, b, c} that is an odd length with exactly one a. Here is my best attempt so far:

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

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

Any hints?

like image 859
ubiquibacon Avatar asked Sep 02 '10 09:09

ubiquibacon


People also ask

How do you determine if a string is odd or even?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .

How do I check if a string is in regular expressions?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.


1 Answers

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

Both prefix and suffix can be zero length. If not, they have to be either both even or both uneven. This means you have two main cases.

EVENPREFIX a EVENSUFFIX | UNEVENPREFIX a UNEVENSUFFIX

Try this (incomplete and wrong):

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

There is still one uneven case missing: a single [bc]:

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

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

  • a
  • cac
  • ccabb

I expect it matches the rest too...

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

Kobi came up with this refinement of the above:

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

How does this work?

The first group is guaranteed to be even. The second group is guaranteed to be uneven with a single a inside. The third group is guaranteed to be be even. Thus, the whole is guaranteed to be uneven.

like image 130
Daren Thomas Avatar answered Oct 12 '22 11:10

Daren Thomas