Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all regular expressions halt?

Is there any regular expression that will, for some input string, search for a match forever?

like image 403
Tom Lehman Avatar asked Aug 06 '09 20:08

Tom Lehman


1 Answers

For a finite input, there is no formal regular expression that will not halt.

Any formal regular expression can be translated into a Deterministic Finite Automata. A DFA reads the input one character at a time, and, at the end of the input, you are either in an accepting state or in a non-accepting state. If the state is accepting, then the input matches the regular expression. Otherwise, it doesn't.

Now, most "regular expression" libraries support things which are not regular expressions, such as back references. As long as you keep away from those features, and have a finite input, you are guaranteed a halt. If you don't... depending on exactly what you are using, you might well not be guaranteed a halt. Perl allows arbitrary code to be inserted, for instance, and arbitrary, turing-machine equivalent code is not guaranteed to halt.

Now, if the input is infinite, then trivial regular expressions can be found which will never halt. For example, ".*".

like image 159
Daniel C. Sobral Avatar answered Oct 22 '22 10:10

Daniel C. Sobral