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.
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.
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.
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}]
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.
Can't we just keep it simple? Just 'if not' this regex:
/(aa|bb|cc)/