Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Exact pattern match?

by oakley (Scribe)
on Jan 19, 2001 at 01:38 UTC ( [id://52846]=perlquestion: print w/replies, xml ) Need Help??

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

Im trying to match either the word "yes" or the word "no" in its exact form... so far, no luck...

How in the world do I go about doing this? The current regex I have is allowing things like "yessir" and "nope" when I want to force it to only be either yes or no...

Here is the snippet:
while ( ($addques{$key}{answer} eq "") || ($addques{$key}{answer} !~ +/$addques{$key}{format}/) ) { print " Invalid answer/option - try again\n"; print "$addques{$key}{question}"; chomp($addques{$key}{answer} = <STDIN>); }
Where the format is nothing more than "yes|no"

Thanks for any help that can be provided - yall rock =)

Replies are listed 'Best First'.
(jeffa) Re: Exact pattern match?
by jeffa (Bishop) on Jan 19, 2001 at 01:45 UTC
    Remember that /yes/ will match 'yes' as well as 'yessir' and also 'grayest' - what you need is word boundries: \b

    So change "yes|no" to '\byes\b|\bno\b' - and notice the single quotes - NOT double quotes.

    And we don't rock - we RAWK!!

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: Exact pattern match?
by chipmunk (Parson) on Jan 19, 2001 at 01:47 UTC
    $string =~ /^(?:yes|no)\z/; \z, unlike $ or \Z, won't match before a newline at the end of the string. If you've already chomped your data or don't care about a final newline, you can use $ instead.

    Refer to perlre for more on anchors in regular expressions.

Re: Exact pattern match?
by swiftone (Curate) on Jan 19, 2001 at 01:44 UTC
    Two options. You could either replace your regex with an eq operation, or you could use the ^ and $ patterns in your regex, which match the beginning or the end of the line. Since you're looking for something terribly exact, I'd recommend using eq.
Re: Exact pattern match?
by c-era (Curate) on Jan 19, 2001 at 01:45 UTC
    If you are looking for 'yes' or 'no' I would suggest doing an 'eq'. If you need to get yes or no out of 'yes I can' or 'no I can't' then you may want to add some white space checking to your regex /(^|\s)(yes|no)(\s|$)/. The 'eq' will be faster.
Re: Exact pattern match?
by adamsj (Hermit) on Jan 19, 2001 at 02:48 UTC
    The other advice is good, but I'll throw in a question:

    Do you have to allow for uppercasing, like Yes and No? If so, you've got some additional work to do.

    There are many ways to do it--my suggestion is to either lowercase the input with lc() before testing (if you're using eq) or to use the /i switch (if you're using the =~ operator). There are other ways, but I'd suggest these.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found