Often we are asked to write regular expression for the set of words that contain a certain string pattern, like $aba$. But what about the opposite?
Is there any generic way to write a regular expression for the set of all words that do not contain a certain pattern?
Asked
Active
Viewed 2,075 times
2

FrankW
- 6,589
- 4
- 26
- 42

Sanjay Singh
- 71
- 1
- 3
-
See also http://cs.stackexchange.com/q/66513/755 – D.W. Nov 26 '16 at 18:51
2 Answers
4
Maybe the easiest (though not necessarily fastest, if done by hand) way to do this is to make use of the many equivalent formalisms for regular languages:
- Create an NFA that accepts all words that contain the pattern. (This NFA will consist of a chain of transitions that are labeled with the pattern and additionally can loop in the initial or final state on every symbol.)
- Transform that NFA into a (complete) DFA using a standard algorithm.
- Make all the accepting states of the DFA non-accepting and vice versa.
- Translate the inverted DFA into a RE using a standard algorithm.
0
It's not clear from the question whether you want to know if such a regexp exists, or to find a small regexp.
If you just want to know whether such a regexp exists, the answer is yes; it follows from closure properties for regexps, such as that the complement of a regular language is regular.
If you want to find a minimal regexp, or a small regexp, see Is regex golf NP-Complete? or Smallest DFA that accepts given strings and rejects other given strings.
-
3I think the question asks for a generic procedure to generate any RE for the language, not necessarily a short one. – FrankW Sep 12 '14 at 09:30