Jaelebi
Jaelebi

Reputation: 6119

regular expression for non consecutive characters

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.

Upvotes: 0

Views: 5632

Answers (4)

Henrik Paul
Henrik Paul

Reputation: 67703

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.

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58441

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.

Upvotes: 4

gacrux
gacrux

Reputation: 960

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

/(aa|bb|cc)/

Upvotes: 2

eKek0
eKek0

Reputation: 23289

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}]

Upvotes: 1

Related Questions