Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Regex for last digit should not be "2"

by Anonymous Monk
on Aug 06, 2012 at 05:49 UTC ( #985601=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to match $folder to M2000AAMFAD201!2(last digit should not match digit "2"),can somone tell me how can I do that?

if ($Folder =~m/M2000AAMFAD201!2/)

Comment on Regex for last digit should not be "2"
Download Code
Re: Regex for last digit should not be "2"
by davido (Archbishop) on Aug 06, 2012 at 05:54 UTC

    if( $Folder =~ m/M2000AAMFAD201[013-9]\z/ ) {...

    Dave

Re: Regex for last digit should not be "2"
by Kenosis (Priest) on Aug 06, 2012 at 07:02 UTC

    ...last digit should not match digit "2"...

    If the last character in the string you want to match will always be a digit, then the following will work:

    if ($folder =~ /^M2000AAMFAD201[^2]$/){ ...

    Hope this helps!

Re: Regex for last digit should not be "2"
by tobyink (Abbot) on Aug 06, 2012 at 07:14 UTC

    You've already received two suggestions meaning "not 2". Here's what the difference between them is:

    [013-9]

    This means "either 0, or 1, or anything between 3 and 9".

    [^2]

    This means "anything other than 2", so while it would not match "2" it would match non-numeric characters like "x".

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Regex for last digit should not be "2"
by Anonymous Monk on Aug 06, 2012 at 14:28 UTC

    Perhaps the simplest way is to test for a string that does not match (that is: !~) a regex like /2$/ ... which looks for the digit 2 anchored to the right-hand side of the string ... or maybe /2\s*$/ which would permit zero-or-more whitespace characters following the 2.

    Go ahead ... use more than one test in that if-statement ... Look for a string that does match one regexp, and that does not match another.   As long as what you write works well and is abundantly clear, you are good to go.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-09-17 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (66 votes), past polls