-1

How to express a regular expression over the alphabet {a, b, c} that doesn't contain the contiguous sub-string baa?

Shangchih Huang
  • 319
  • 3
  • 11

1 Answers1

2

If your regex flavor supports negative lookaheads, then it's relatively simple. E.g. in php it looks like this:

^^(?:(?!baa)[abc])*$

Demo here.

Explanation:

  • ^...$ makes sure we match the entire line
  • [abc] is a character class that defines the alphabet
  • (?!baa) is the negative lookahead. It checks for every position if it is followed by baa. If it is, then it's not a match
  • finally, we group these two with a non-capturing group: (?:...) and repeat them as many times as fits into the line: (?:...)*

Update

Updated the demo and the regex according to ClasG -s comment. Indeed, to make sure it fails for a simple baa, the lookahead must come first, then the character class.

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49