print $s =~ /^[xyz]*$/ ? "$s matches\n" : "$s does not match\n";

That snippet is quite inefficient, and will fail to work sometimes.

First, the '*' quantifier does too much work. Look for a second at what '*' is going to do. Let's assume the following string:

my $string = "xyzzyzx";

When you test that with your regexp, because of the greedy '*' quantifier placed between a ^ and $ beginning of string and end of string zero-width assertion, you get this behavior:

$string =~ /^[xyz][xyz][xyz][xyz][xyz][xyz][xyz]$/;

In other words, the regexp engine uses the '*' quantifier to build up a very big chain of character classes, which is essentially the same as saying /^[xyz]{7}$/. The longer the string you're scanning, the more [xyz] character classes are built up into the regexp.

The second problem is that /^[xyz]*$/ will match a string of zero length, or an empty string. Here's why. The '*' quantifier will match zero or more times. The ^ and $ assertions tell the regexp engine that your regexp must match from the beginning to the end of the string. Fine. So if the string contains only one 'x', you've got a match. If it contains many characters from the [xyz] character class, you've also got a match. But you know what? If you have NO characters, the '*' does just as it's supposed to do, and matches successfully. And ^ and $ are happy, because their requirement has been met as well; the string has been matched from start to finish (even though there's nothing there at all).

Your regexp would at least work (with less than optimal performance) if you change the '*' quantifier to a '+' quantifier. Then it would be on track.

I posted an even more efficient solution here: Re: matching all characters within a string?.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: Re: matching all characters within a string? by davido
in thread matching all characters within a string? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.