The original 32K regex pointed to in the stackexchange problem statement however is perfectly correct.
Test code:
open(F, '< /tmp/q.in');
$myre = <F>;
chomp($myre);
$myrecomp = qr/^(?:$myre)$/;
while (<>) {
chomp();
if (m/$myrecomp/) {
print "$_\n";
}
}
Examples of multiples of 7 not matched by the given regex: 70, 707, 7007, 11095(I spent all morning trying to find the bug in my regex compiler based on the assumption that the answer there was correct)
I wonder if that's the optimal regex or can be reduced further using some rules[1] like the ones which we(humans) generally use to do it manually.
(I have a regex -> DFA compiler here that I just threw your regex through. Seven states, connected as expected)
(I think I've read that finding the smallest one is not only NP-hard, but P-Space complete.)
Of (a \star b \star ) \star and ( a ∪ b ) \star, which is the canonical form? The transformation from the former to the latter is nontrivial as a \star b \star is not equivalent with a ∪ b.
It remains PSPACE-complete if you're given a corresponding DFA: "Jiang and Ravikumar [7] show moreover that the minimization problem for ... regular expressions remains PSPACE-complete, even when specifying the regular language by a dfa" from http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.60....
In that sense, the grandparent is incorrect. It is unknown, in both practice and theory, how to find the shortest equivalent regex.
Heck, there might be an algorithm that works really well in practice. People who do model checking and verification laugh PSPACE completeness in the face and are not even really concerned with undecidability.
It also seems that regexes can't express accepting expressions like AB where A is accepted by itself (considering AB is accepted), which in the DFA is implemented by sending it to the initial state
B* does that.
Where is this going? Behold, a 12,701 character regex for divisibility by 7: http://codegolf.stackexchange.com/questions/3503/hard-code-g...
This doesn't use any recursive features (it is a real Regular Expression in the theoretic sense) so it can be checked in linear time.