in reply to Converting RegExs.

Perhaps you are worrying about something that isn't a problem. Witness:
my $pat = "/etc/"; my $string = "/usr/etc/config"; if ($string =~ m/$pat/x) { print "it matches\n"; }
This works just fine. The included variable contents in $pat cannot cause the delimiters to be prematurely recognized; that phase is already long past.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: Re: Converting RegExs.
by dcorbin (Sexton) on Sep 02, 2000 at 23:41 UTC
    Oh. Silly me. DWIM is such a good concept. Thanks.
RE: Re: Converting RegExs.
by BlaisePascal (Monk) on Sep 04, 2000 at 09:40 UTC
    Hmm,

    What if the user entered m/foo/x when asked for a regex? Technically, the regex the user wanted was foo, but they entered the whole match command instead:

    my $keywords = "NSA|cocaine|Lybia|assasin(at(ion|e))?|CIA"; print "Enter regex matching other keywords:"; my $userwords = <STDIN>; # user enters "m/MI-6/x" if ($string =~ m/$keywords|$userwords/x) { print "Found a terrorist! $string\n"; }
    Obviously, m/MI-6/x (they want to match "MI-6", not "m/MI-6/x"). How could that be corrected for?
      Obviously, m/MI-6/x (they want to match "MI-6", not "m/MI-6/x"). How could that be corrected for?
      By first answering the question: "how will you know they entered a piece of Perl code instead of simply the regular expression?"

      Most of programming is asking yourself how you know something. The code to teach the machine to know it falls out from that.

      So, how would you know whether they entered a piece of Perl code instead of a regex? If you decide arbitrarily that it's when something begins "m slash", then there's your solution: use a regex to extract the piece. Maybe they select another form element that says "I'm entering a regex instead of a piece of Perl code", or something. But an arbitrary string is neither a regex nor a piece of Perl code (maybe they wanted to match something that begins with m slash).

      Of course, the simplest way to correct for it is with humanware instead of DWIM-ware. Put a big message by the input box that says "enter a regex, not a piece of Perl code!". Sometimes, the simplest solutions are the best.

      -- Randal L. Schwartz, Perl hacker

        And, of course, you can compile the regex that they entered using qr//. You should run that in an eval and if it fails then you know that they've entered and invalid regex and you can go back and ask for a better one.

        --
        <http://www.dave.org.uk>

        European Perl Conference - Sept 22/24 2000, ICA, London
        <http://www.yapc.org/Europe/>
        BlaisePascal actual hit it on the head. I'm not sure what drugs I was on Saturday to so blindly accept what you said. What I've really got is input from RecDescent parsed language. One of the things that I accept in my language is a "perl pattern match". For conveniece sake, I want to permit the user to specify whatever delimeters they want. Then, one or more patterns are joined to together, and I let perl regex engine do the hard work. I think tilly's suggestion of qr// is probably the right call. I can apply that "early on", and know that the escaping in the RE has been properly applied.