Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I would like to match exactly 6 digit if user inputs from the command +line. The following code I used, but if I go for more than 6 digits its gett +ing fail, I am getting Exact match. Can anyone look into this ======================= 6 digit - Exact match expect that should display Not Correct ================= $d=$ARGV[0]; if($d =~ m/\d{6}/) { print "Exact match\n"; } else { print "Not Correct\n"; }
Thanks

Replies are listed 'Best First'.
Re: Don't know how to resolve
by svenXY (Deacon) on Oct 04, 2007 at 10:09 UTC
    Hi,
    chomp $d; if($d =~ m/^\d{6}$/) { ### use ^ and $ for start and end print "Exact match\n"; } else { print "Not Correct\n"; }
    should do the trick.
    8 digits will contain (a.k.a. match) 6 digits and therefore succeed.
    update: chomp your input to remove the trailing newline.
    Regards,
    svenXY
      thanks
Re: Don't know how to resolve
by mwah (Hermit) on Oct 04, 2007 at 10:18 UTC
    AnonymousThe following code I used,
    but if I go for more than 6 digits its getting fail,

    Whats meant by "more/less than six digits"?

    Would this: "123456abcde" be acceptable?
    Or this one: "abc123456def" ? Or only this: "123456" ?

    If *not*, you need anchored match, ^ => "line start", $ => "line end"
    otherwise, you can use lookaround assertions (?...)
    ... my $d = shift; # $ARGV[0] if( $d =~ /(?<!\d)\d{6}(?!\d)/ ) { print "Exact match (lookaround)\n +" } else { print "Not Correct\n" } if( $d =~ /^\d{6}$/ ) { print "Exact match (anchored)\n" } else { print "Not Correct\n" } ...
    Regards

    mwa
Re: Don't know how to resolve
by arkturuz (Curate) on Oct 04, 2007 at 10:12 UTC
    Try this one:
    if($d =~ m/^\d{6}$/) { print "Exact match\n"; }