Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What Im trying to do is allow a user to specify a regex through form submission, then have the regex run on a piece of data.

In terms of processing the regex I've tried the following with now luck
my $str = "Hello my name is Kevin"; my $regex = $form->query(...); #Value of regex : "/Hello\smy\name\sis\s(\w.*)/" $str =~ $regex;
I've also tried the string statement as the following
$str =~ /$regex/; #where omitting / in $regex eval($str =~ /$regex/); eval($str =~ $regex);
Not one of the above returns a value for $1 Any ideas, the regex works fine on its own when used in a traditional method

Replies are listed 'Best First'.
Re: Regex from a string
by dragonchild (Archbishop) on Jun 17, 2005 at 14:56 UTC
    You're looking for qr//.

    Note: what you're trying to can be a really bad thing. The reason is because regexes can theoretically execute arbitrary Perl code using the (?{...}) construct. Now, use re 'eval'; has to be in effect, but if that gets put in by a maintenance programmer later, you're in trouble.

    Update: Link to apparently pirated material removed.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Shame on you dragonchild for linking to pirate O'Reilly books.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
        Ug. That's the exact same site that got me recently, too.. A quick google search on this topic found it very high in the results here, too..
Re: Regex from a string
by TedPride (Priest) on Jun 17, 2005 at 15:38 UTC
    Well, this works fine:
    $_ = 'Hello my name is A0'; my $r = 'Hello\smy\sname\sis\s(\w.*)'; print $1 if m/$r/;
    But I can't figure out how to let the user set flags like i. Eval is generally a bad idea unless you know exactly who is going to be using your script.