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

Colleague:

I am a “newbie” to the Perl world so please be patient with me. I am trying to understand why the following code snippet works as it does. As written, it gives “NOT a match”, which is the expected “textbook” behavior. But if B* includes zero matches, why isn’t the “C” a match? Even with the anchors I do not understand.

Many thanks, Kevin

$_ = 'C'; if ( /^B*$/ ) { print ' a match ...', "\n"; } else { print ' NOT a match ...', "\n"; }

Replies are listed 'Best First'.
Re: Anchors and zero matches question
by kennethk (Abbot) on Jan 07, 2011 at 22:58 UTC
    The reason is the anchors ^ and $. As revealed by YAPE::Regex::Explain:

    NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- B* 'B' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    In order for a match to occur, all characters between the start and end of the string must be 'B' (with the possible addition of a trailing newline). See perlretut.
Re: Anchors and zero matches question
by state-o-dis-array (Hermit) on Jan 07, 2011 at 23:02 UTC
    As already explained, it's having both anchors. If you only have one of either of the anchors it will match. If you have both, it will only match an empty string or 1 or more "B".
Re: Anchors and zero matches question
by oko1 (Deacon) on Jan 08, 2011 at 01:51 UTC

    The regex says "Match any number of 'B's, including none, from the beginning of the line to the end." So, an empty line would match; so would a line containing nothing but a single 'B'; ditto for a string consisting of nothing but 'B's of any length - but that's the entire range of what that regex will match. 'C' isn't one of the above choices, so it doesn't match.

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats