Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Regular Expression match too much

by enemyofthestate (Monk)
on Feb 08, 2006 at 19:34 UTC ( [id://528906]=perlquestion: print w/replies, xml ) Need Help??

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

I have an perl web application that has to parse data from a customer for 'correctness'. One of the checks I use is something like:
$criteria = "HE|NP|OT|RF|SM"; if ($customer_data =~ m/$criteria/) { process_as_good() } else { proc4ess_as_bad() }
Obviously this matches for $customer_data = HE, NP, OT, RF, or SM which is the desired behavior. Unfortunately it will also match combinations like SMNP which is not desired. I seem to recall reading about a technique for this but I cannot find it again. Is it just my faulty memory or is there a clever way to make Perl match on the entire string and not just on a substring?

Replies are listed 'Best First'.
Re: Regular Expression match too much
by ikegami (Patriarch) on Feb 08, 2006 at 19:36 UTC

    m/^(?:$criteria)\z/
    Ref: perlre

      N.B.: If $criteria were a precompiled regex rather than a string being interpolated, the grouping parentheses would not be necessary.

      Caution: Contents may have been coded under pressure.
      m/^(?:$criteria)\z/
      That was it! Thank you.
      Just out of curiostiy, why did you use \z and with ^ instead of \A?

      thor

      The only easy day was yesterday

        Little code &mdash I don't recall ever seeing any &mdash uses \A and \Z for regexps without /m. This is probably because the alternatives ^ and $ stand out more. I didn't see any reason for breaking this convention. If \a was an alias for \A, I would have used that.
Re: Regular Expression match too much
by GrandFather (Saint) on Feb 08, 2006 at 19:44 UTC

    m/\b$criteria\b/ - require a word break at each end of the text. Allows matches on 'SM and other stuff' or 'SM NP', but not on 'SMNP'.


    DWIM is Perl's answer to Gödel

      Nice tip, but that should be m/\b(?:$criteria)\b/.

      print('BOTTOM' =~ m/\b$criteria\b/ ?1:0, "\n"); # 1 print('BOTTOM' =~ m/\b(?:$criteria)\b/ ?1:0, "\n"); # 0

        Hmm, sneeky. I come here to learn, I learned :). Thankyou.


        DWIM is Perl's answer to Gödel
Re: Regular Expression match too much
by Cody Pendant (Prior) on Feb 09, 2006 at 00:09 UTC
    Random thought -- if it's a web form you could set the maximum characters of the field to 2. That would diminish the possibilities for error.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
      The data is parsed from an XML document. I have no control over what the customer puts in it so I have to do a lot of sanity checking

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found