According to perlldoc - I should not get a match if $word is say 509992. It should only match if it is at least 2 digits and no more than 4. See this snippit:
my $word = $ARGV[0];
print "It matches\n" if $word =~ /\d{2,4}/;
Yet if I feed this something whacky like 50401030303 - it WILL match and print out "It matches". Is this guy's perlrequick document just wrong on this point or have I futzed something up?
http://perldoc.perl.org/perlrequick.html
2006-09-11
18:52:01
·
2 answers
·
asked by
HomeSweetSiliconValley
4
in
Computers & Internet
➔ Programming & Design
Good thought...but even one digit over matches in appropriately:
23:03:48 $ ./matchYear 19996
It matches
2006-09-11
19:05:06 ·
update #1
that is - inappropriately. :-)
2006-09-11
19:05:39 ·
update #2
Haha...even this matches:
23:06:40 $ ./matchYear 123457812349871324981743098174309871324HOHOHO
It matches
If I feed it only 1 digit or no digits it's a no-match or error respectively.
2006-09-11
19:07:59 ·
update #3
Ok - here's the answer from a helpful usenet subscriber:
It does not say that the
entire pattern will not match if $word contains more than 4 digits. It
says that THAT TOKEN will not match more than 4 digits. To better see
what's happening:
print "Before: '$1', Match: '$2', After: '$3'\n" if $word =~
/(.*?)(\d{2,4})(.*)/;
That will place whatever \d{2,4} matched into $2, and everything before
that in $1 and everything after that in $3.
If you really want to only match if the string contains a max of 4
digits, you have to insure that the digit string is neither preceded
nor followed by a digit:
print "It matches\n" if $word =~ /(?
(Look up in that documentation the section on Look-behind and
Look-ahead assertions).
2006-09-12
06:54:52 ·
update #4