http://www.perlmonks.org?node_id=531683


in reply to Testing regex equivalence

you could use "b" or whatever it is to compile a perl regex into token form. equivalent regular expressions (e.g. /.../ and /.{3}/ should end up the same. as for more complicated regular expressions, they may end up being equal depending on the dataset being fed in. the only way to test this kind of equivalence is to run both on the dataset in question.
the hardest line to type correctly is: stty erase ^H

Replies are listed 'Best First'.
Re^2: Testing regex equivalence
by grinder (Bishop) on Feb 21, 2006 at 15:25 UTC
    equivalent regular expressions (e.g. /.../ and /.{3}/ should end up the same

    Evidently, you did not try this out yourself before posting. It took me 10 seconds to see that

    % perl -Mre=debug -e '/.../' Freeing REx: `","' Compiling REx `...' size 4 Got 36 bytes for offset annotations. first at 1 1: REG_ANY(2) 2: REG_ANY(3) 3: REG_ANY(4) 4: END(0) minlen 3 Offsets: [4] 1[1] 2[1] 3[1] 4[0] Freeing REx: `"..."'

    and

    % perl -Mre=debug -e '/.{3}/' Freeing REx: `","' Compiling REx `.{3}' size 4 Got 36 bytes for offset annotations. first at 3 1: CURLY {3,3}(4) 3: REG_ANY(0) 4: END(0) minlen 3 Offsets: [4] 2[3] 0[0] 1[1] 5[0] Freeing REx: `".{3}"'

    are quite different beasts. So that's not going to fly. Unfortunately, I don't have any better ideas myself. The only thing I can think of would be to take a look at japhy's Regexp::Parser, which will ease the pain of picking a pattern apart, but to put them back together again with an eye to checking for equivalence will be non-trivial.

    • another intruder with the mooring in the heart of the Perl