Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RE: Re: Converting RegExs.

by BlaisePascal (Monk)
on Sep 04, 2000 at 09:40 UTC ( [id://30968]=note: print w/replies, xml ) Need Help??


in reply to Re: Converting RegExs.
in thread Converting RegExs.

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?

Replies are listed 'Best First'.
RE: RE: Re: Converting RegExs.
by merlyn (Sage) on Sep 04, 2000 at 17:53 UTC
    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/>
        And when they wonder why /5/ doesn't match 5?
        perl -e '$in = "/5/"; $m = qr/$in/; print 1 if 5 =~ $m'
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://30968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found