[Chicago-talk] using a regex in a variable
Steven Lembark
lembark at jeeves.wrkhors.com
Mon Oct 27 07:42:14 CST 2003
--On Friday, October 24, 2003 09:25:50 -0500 Ed Summers <ehs at pobox.com>
wrote:
> On Fri, Oct 24, 2003 at 09:16:23AM -0500, Jay Strauss wrote:
>> Because I need to be able to dynamically use a different regex (which I
>> look up in my db) under different conditions
>
> So let's say you've got this regex stored in your DB.
>
> s/SCO/IBM/
>
> and you've extracted it, stored it in $regex, and you want to match it
> against $string.
>
> eval( '$string =~ ' . $regex );
>
> should do the trick. I was wondering if there was anything like qr// for
> substitution but a quick glance in the Camel didn't turn up anything.
You can use the qr for what gets matched, not the entire replacement.
The database might be nice for storing a shared set of regexen:
my $source = $dbh->fetchall_arrayref( ... );
my %rxen = map { @$_ } @$source;
$_ = qr/$_/ for values %rxen;
allows you to store the rx types and their replacemnet text in the
database then apply them quickly.
If there are a lot of rxen then it might pay to qr them on the fly
to avoid wasting time on unused ones:
my $source = $dbh->fetchall...
my %rxen = map { ... } @$source;
...
$rxen{$type} = qr/$rxen{$type}/ unless ref $rxen{$type};
...
--
Steven Lembark 2930 W. Palmer
Workhorse Computing Chicago, IL 60647
+1 888 910 1206
More information about the Chicago-talk
mailing list