[Chicago-talk] using a regex in a variable

Steven Lembark lembark at jeeves.wrkhors.com
Mon Oct 27 07:37:03 CST 2003



--On Friday, October 24, 2003 09:16:23 -0500 Jay Strauss <me at heyjay.com> 
wrote:

> Oh, I can do that... Just joking
>
> Because I need to be able to dynamically use a different regex (which I
> look up in my db) under different conditions

Look up compiled regexen in the camel. You can get the matching portion
stored for faster use in something like a hash and then use it to
dynamically look for something that has to be replaced:


    my %rxen =
    (
        type1 => qr/look for this/oi,
        type2 => qr/look for that/os,
    );

    ...

    $input =~ s/$rxen{$type}/$replacewith/;

allows you to apply a regex chosen by the $type variable and replace
whatever it finds with the contents of $replacewith.

-- 
Steven Lembark                                            2930 W. Palmer
Workhorse Computing                                    Chicago, IL 60647
                                                         +1 888 910 1206



More information about the Chicago-talk mailing list