Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Deregex

by Dylan (Monk)
on Aug 30, 2002 at 22:19 UTC ( [id://194268]=CUFP: print w/replies, xml ) Need Help??

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2025-06-23 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.