http://www.perlmonks.org?node_id=109316


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