tags:

views:

466

answers:

4

If a language consists of set {a, b, c} only how can we construct a regular expression for the langage in which no two consecutive characters appear.

eg: abcbcabc will be valid and aabbcc will rejected by the regular expression.

+1  A: 

Assuming "()" is a grouping notation, and "a|b" stands for a logical-or b, then, in pseudocode

if regexp('/(aa)|(bb)|(cc)/', string) == MATCH_FOUND
  fail;
else
  succeed;

Probably doesn't need the grouping, as Gumbo said. I have them there just to be safe and clear.

Henrik Paul
You don’t need the grouping.
Gumbo
+1  A: 

You must match the input against something like this (coded in whatever you want), and if you found a coincidence then it is the language you want:

[^{aa}|{bb}|{cc}]
eKek0
+3  A: 

This regular expression matches abcbcabc but not aabbcc

// (?:(\w)(?!\1))+
// 
// Match the regular expression below «(?:(\w)(?!\1))+»
//    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
//    Match the regular expression below and capture its match into backreference number 1 «(\w)»
//       Match a single character that is a “word character” (letters, digits, etc.) «\w»
//    Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!\1)»
//       Match the same text as most recently matched by capturing group number 1 «\1»


Edit

as has been explained in the comments, string boundaries do matter. The regex then becomes

\m(?:(\w)(?!\1))+\M

Kudos to Gumbo.

Lieven
Don’t forget the string boundaries.
Gumbo
@Gumbo, +1 for reminding me but I doubt it would be necessary. What would get passed to the check is the set, never an entire block of text.
Lieven
If you don’t mark the start an end of the string, `abb` would also be matched.
Gumbo
@Gumbo, you are right. I edited the answer.
Lieven
+2  A: 

Can't we just keep it simple? Just 'if not' this regex:

/(aa|bb|cc)/
gacrux