Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery


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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-07-24 19:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.