English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2 answers

Interesting and weird. If you're trying to use this for matching years, then the other example in PerlDoc is better, anyway:
$year =~ /\d{4}|\d{2}/; # better match; throw out 3 digit dates

I'm using ActiveState PERL v5.8.7

I've been playing with this for a bit, and can only get minimum digit matches to work. So for /\d{3}/, 1 or 2 digit inputs fail, but 3 or more digits works.

2006-09-11 19:23:19 · answer #1 · answered by Jeff S 2 · 0 0

Its possible that you are overflowing the varibale. Meaning that the size of the number you put in is too big. Try five digit number, then six and so on. And See what happens, see where it breaks.


Good Luck

2006-09-11 19:03:18 · answer #2 · answered by demaman 3 · 0 0

fedest.com, questions and answers