Lloyd Zusman
10/20/2004 12:06:00 AM
James Edward Gray II <james@grayproductions.net> writes:
> On Oct 19, 2004, at 1:53 PM, Jamis Buck wrote:
>
>> Is there a way to test regexen for equality based on the strings they
>> would match?
>
> Wow. I bet that's a pretty tall order. Pulling from the current Ruby
> Quiz, here are several ways to write a regex to match 1..12:
>
> 1|2|3|4|5|6|7|8|9|10|11|12
>
> \d|1[012]
>
> 1?[12]|[03-9]
>
> You're certainly not going to be able to compare like patterns that way.
> Maybe some internal representation, but I would but super impressed to
> see that.
>
> Not at all saying I don't like the idea, just that it's a tall order.
Not only is it a tall order to determine whether any two regex's will
match exactly the same set of strings, but in the general case, I'm
pretty sure that this question is undecidable.
If I'm not mistaken, I believe that this can be shown to be a reduction
of the Halting Problem.
In other words, short of feeding every possible character string to both
regex's and comparing the two sets of matches, I don't think that there
is an algorithmic way to measure equivalence of two regex's which don't
have the same external representation.
--
Lloyd Zusman
ljz@asfast.com
God bless you.