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

Replies are listed 'Best First'.
Re: Regex for last digit should not be "2"
by davido (Cardinal) 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 tobyink (Canon) 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 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 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.