in reply to Re: Dynamic regexp from array values
in thread Dynamic regexp from array values

You could either build a regex
my $regex = join '|', map "\Q$_\E", @vals; doSomething() if $data =~ $regex;

Actually my $regex = qr(join '|', map "\Q$_\E", @vals) will optimize the pattern matching. See perldoc -f qr

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: Re: Re: Dynamic regexp from array values
by broquaint (Abbot) on Feb 12, 2003 at 12:16 UTC
    Actually my $regex = qr(join '|', map "\Q$_\E", @vals) will optimize the pattern matching
    Hrm, that's debatable, check out diotalevi's reply in meaning of /o in regexes.
    HTH

    _________
    broquaint

Re: Re: Re: Dynamic regexp from array values
by ihb (Deacon) on Feb 12, 2003 at 20:08 UTC
    Actually my $regex = qr(join '|', map "\Q$_\E", @vals) will optimize the pattern matching

    Unfortunately I'll be a totally different pattern though. Dispite the looks of it, qr() is not a function call. It's a quote operator with () as delimiters. You need to either interpolate the join() call (with e.g. @{[join ...]}), or just do $regex = qr/$regex/.

    Update: Had misplaced the parenthesis.

    ihb