Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number


by Dylan (Monk)
on Aug 30, 2002 at 22:19 UTC ( #194268=snippet: print w/replies, xml ) Need Help??
Description: Decompile regexes made with qr//
# Thanks go to Arien for simplifying and enhancing :)
sub deregex {
    my $regex = shift;
    my ($ops, $pat) = $regex =~ /^\(\? ([msix-]+) : (.*) \)$/x
        or die "Unknown qr// format";

    $pat =~ s!/!\\/!g;
    $ops =~ s/-.*//;
    return "/$pat/$ops";
Replies are listed 'Best First'.
Re: Deregex
by Arien (Pilgrim) on Aug 30, 2002 at 23:00 UTC

    You're missing a $ in my (regex) = @_. Also, you can qr// an empty string, which will break your regex.

    Simplifying and enhancing a bit more results in something like:

    sub deregex { my $regex = shift; my ($ops, $pat) = $regex =~ /^\(\? ([msix-]+) : (.*) \)$/x or die "Unknown qr// format"; $pat =~ s!/!\\/!g; $ops =~ s/-.*//; return "/$pat/$ops"; }

    — Arien

      Oopsy. I must've removed it when I removed the $self, from it (It was a method). Thanks a lot!
Re: Deregex
by belg4mit (Prior) on Aug 31, 2002 at 01:10 UTC
    But a qr already stringifies as a valid regexp. Unless your intent is to make something which can be passed back into qr, which is not the same thing, but that doesn't seem to be the case.

    perl -pew "s/\b;([mnst])/'$1/g"

      It's for error messages, actually. "... doesn't match ..." type thing.
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://194268]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2023-06-04 03:38 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (17 votes). Check out past polls.