in reply to Decompiling Regular Expressions

I'm not quite sure what you mean by "pulling out expressions", but you might want to know that Regexp::Common groups the patterns in classes, and you can selectively load the classes. For instance:
use Regexp::Common qw /number URI/;

only loads the number and URI patterns. Currently, there are 11 different classes.

From your post, I didn't get any idea what you mean by "getting the source form of a regex" in relation to Regexp::Common, nor what YAPE::Regex::Explain is doing for you.

Abigail

Replies are listed 'Best First'.
Re: Re: Decompiling Regular Expressions
by shotgunefx (Parson) on Apr 03, 2003 at 21:10 UTC
    My question I'm sure could have been phrased better. Basically what I was thinking is there are certain times when you might want to use an expression without requiring the module. (Normally I would just want to stick with "use"ing it and getting the benefit of the module's maintainence), so I was curious if there was a way to go from a compiled expression to something I could add to a script without requiring the module for the one regex.

    A simple example but if I wanted a pattern to match a real but didn't want to require Regexp::Common I could do something like this.
    #!/usr/bin/perl use warnings; use strict; use Regexp::Common qw/number/; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new($RE{num}{real})->explain(); __END__ The regular expression: (?-imsx:(?:(?i)(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?: +[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|) +)) matches as follows: NODE EXPLANATION ------------------------------------------------------------BLAH BLAH +BLAH
    and just add
    $thing=~/(?-imsx:(?:(?i)(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789 +]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456 +789]+))|))) /
    to my code.

    Which seems like it works but as regex's are my weakest Perl strength, I didn't know if this is a any caveats (things known not to work) with this approach or if there is a more reliable way of doing it. Obviously "cutting and pasting" is a bad idea without total understanding is in general though as far as regexes, I'm willing to put stock in a few monks work (yourself included), but I didn't know if there was anything inherently wrong with the approach.

    -Lee

    "To be civilized is to deny one's nature."
      That's like going from London to Greenwhich via Sydney. Why not just:
      perl -MRegexp::Common -wle 'print $RE{num}{real}'

      Note that this (or the YAPE::) approach doesn't work with recursive regexps.

      Abigail

        slaps($self);
        I knew I must have been missing something because I couldn't find anyone who had asked. I could have sworn I tried that and got a bunch of binary characters which is why I thought it needed to be decompiled.

        Thanks,
        -Lee

        "To be civilized is to deny one's nature."