Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Reg Expression Question

by azatoth (Curate)
on Aug 31, 2001 at 12:00 UTC ( #109316=note: print w/ replies, xml ) Need Help??


in reply to Reg Expression Question

Update : Semi-wrong answer given by me, azatoth the regex spastic. Off to read Owl book.

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?


Comment on Re: Reg Expression Question
Re: Re: Reg Expression Question
by Kevman (Pilgrim) on Aug 31, 2001 at 12:02 UTC
    Sorry, Cut & pasted code.
    Original Code has square brackets and it doesnt work!!
    Will try the hash idea?
    Thanks All
Re: Re: Reg Expression Question
by Jonathan (Curate) on Aug 31, 2001 at 12:05 UTC
    That would also match 1 which is incorrect.
    $i = 1; print "found\n" if $i =~ /[0|5|8|15|20]/;
Re: Re: Reg Expression Question
by blakem (Monsignor) on Aug 31, 2001 at 12:06 UTC
    $i =~ /[0|5|8|15|20]/

    But that will also match 30,15,888,150,200, etc...

    If you really want to use a regex for this, you'll need to anchor it like so:

    $i =~ /^(0|5|8|15|20)$/;
    Though I still think a hash is a better solution.

    Update: changed the square brackets to parens, thanks Hofmator...

    -Blake

Re: Re: Reg Expression Question
by Hofmator (Curate) on Aug 31, 2001 at 12:07 UTC

    Well, no ... $i =~ /[0|5|8|15|20]/;
    matches one character which is either 0, 1, 2, 5, 8 or |. And only if you correct the typo ;-)

    What you can do is use parenthesis like this $i =~ /^(0|5|8|15|20)$/;
    but make sure you have the start and end of string assertions, otherwise you will match much more numbers.

    Apart from that I think a hash would be a better solution as already mentioned elsewhere.

    -- Hofmator

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-13 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls