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

Does a string of 20 letters followed by an exclamation point match /^\w{6,20}$/

In other words, what will this print:

$s = "xxxxxxxxxxxxxxxxxxxx!"; if($s =~ /^\w{6,20}$/){print "it's a match";} else{print "no match";}

I think there is no match because the ^ and $ make sure that the string contains ONLY between 6 and 20 alphanumerics (and underscores) and nothing else. Am I right?

Update from one of my friends: weirdness... when i try from the cmdline using this (perl -e 'print "yes\n" if (q{a} x 20 . q{!} =~ m/^\w{6,20}$/);', it appears to, but not with this: perl -e '$c = q{a} x 20 . q{!}; print "yes\n" if ($c =~ m/^\w{6,20}$/);'

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: another question on a string of 20 letters
by pKai (Priest) on Feb 04, 2006 at 21:28 UTC
    when i try from the cmdline

    Yes, that's the definite way to decide such nagging questions.

    For the q{a} x 20 . q{!} =~ m/^\w{6,20}$/ being true: This is a consequence of Perl's operator precedence. (See perldoc perlop.) Actually the return value of that command is a string of 20 'a'-s, since the dot in there has the lowest precedence. To the left of the dot we have 'a'x20 to which an empty string (the result of '!' not matching your regexp) is appended.

      More concisely,
      if (q{a} x 20 . q{!} =~ m/^\w{6,20}$/)
      means
      if ((q{a} x 20) . (q{!} =~ m/^\w{6,20}$/))
      (which is always true) and not
      if (((q{a} x 20) . q{!}) =~ m/^\w{6,20}$/)
      due to operator precedence.

Re: another question on a string of 20 letters
by GrandFather (Saint) on Feb 04, 2006 at 21:15 UTC

    You are right and when I run your code using AS Perl 5.8.7 it prints "no match" as expected.

    Your second one liner has opposite sense to your first so it is matching as you expect, just not testing as you expect. Before coffee reply effect


    DWIM is Perl's answer to Gödel