Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Reg Expression Question

by azatoth (Curate)
on Aug 31, 2001 at 12:00 UTC ( [id://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?

Replies are listed 'Best First'.
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

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 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

Log In?
Username:
Password:

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

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

    No recent polls found