Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This post is a little late, but I've just came across Regexp::Compare and remembered your OP. Putting aside the theoretical complaints on complexity consider the following code:
use strict; use warnings; use IPC::Run3; use Regexp::Compare qw( is_less_or_equal ); my @re = ( qr/asdf/, qr/asd/, qr/as/, qr/a/, qr/./, qr/.*/, ); my $stdin = ''; my $stdout = ''; my $stderr = ''; for my $i ( 0 .. $#re ) { for my $j ( 0 .. $#re ) { $stdin .= "$i $j\n" if is_less_or_equal( $re[$i], $re[$j] ); } } # tsort (GNU coreutils) 8.4 run3 [ 'tsort' ], \$stdin, \$stdout, \$stderr; print $stderr; my @idx = split /\s+/, $stdout; print "$_\n" for @re[@idx];

In the output the most specific regex comes first and the most generic comes last. If you put more than one equivalent regexes into your input then you get a nice warning on loops:

my @re = ( qw/./, qw/./, qw/.+/, );
in $stderr: tsort: -: input contains a loop: tsort: 0 tsort: 2 tsort: 1 tsort: -: input contains a loop: tsort: 0 tsort: 2 tsort: -: input contains a loop: tsort: 1 tsort: 2

I've used topological ordering because the more-specific relation on regexes is not a total ordering (I hope I'm using the correct math terms), however this still guarantees that a more specific item comes earlier therefore you won't get the "false" (more generic) match first.

As a sidenote: while playing with this, I've realized that your sample regexes are a little "dirty". Let's take \AHi and \b(Hi(ya)?|Hello|Greetings)\b for instance. None of these two is more specific than the other, because none of the sets of every possible match is a subset of the other. (Consider the strings His and Foo Hi.)

Specifically GNU tsort is probably a better choice than Sort::Topological as it does not die on cyclic graphs.

Of course Regexp::Compare uses heuristics, I suppose something along like this:

hypothesis: $re1 <= $re2 is_less_or_equal( $re1, $re2 ) specific -> $re1 $re2 <- generic if exists $str that $str =~ /$re1/ and $str =~ /$re2/ -> no consequence $str =~ /$re1/ and $str !~ /$re2/ -> falsifies hypothesis $str !~ /$re1/ and $str =~ /$re2/ -> corroborates hypothesis $str !~ /$re1/ and $str !~ /$re2/ -> no consequence
But I cannot tell how well it performs for real regexes.

I hope this is useful for you.

In reply to Re: Analysis of Regular Expressions by rubasov
in thread Analysis of Regular Expressions by PetaMem

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2018-06-21 13:08 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (118 votes). Check out past polls.