http://qs1969.pair.com?node_id=672377


in reply to Compare two regex patterns

from the looks of it, a good solution may be to use Regexp::Optimizer on both values. Then see if they are equal after

Replies are listed 'Best First'.
Re^2: Compare two regex patterns
by ikegami (Patriarch) on Mar 06, 2008 at 15:38 UTC

    That doesn't work for two reasons.

    1. Regexp::Optimizer is a no-op if no alternation is used.

    2. Even if Regexp::Optimizer normalized regexps, the normalized regexps still wouldn't be equal since the two regexps are not equivalent.

    use Regexp::Optimizer qw( ); my $o = Regexp::Optimizer->new(); print $o->optimize(qr/123*/), "\n"; # (?-xism:123*) print $o->optimize(qr/1233*/), "\n"; # (?-xism:1233*) print $o->optimize(qr/123+/), "\n"; # (?-xism:123+)

    The OP didn't ask for a method to check if two regexp are equivalent, but for a method to check if a regexp will match at least everything a second regexp matches.

    Update: Added code. Changed formatting.