Because in the case of "dog:cat" =~ /(cat)*/;, the "*" quantifies the match to "zero or more" and the first match (at the leading "dog") is "cat" zero times.

Here's an illustration that beats to death that aspect of your issue.

#!/usr/bin/perl use Modern::Perl; my $str0 = "0 dog:cat" =~ /(cat)*/; say "\$str0 $str0"; # 1 -- original replaced by scal +ar value my $str1 = "1 dog:cat"; say "\$str1: $str1"; if ($str1 =~ /(cat)*/ ) { my $capture = $1; say "matched |$capture| in \$str1 ($str1) using * quantifier in re +gex"; # see output: uninit $capture }else{ say "no match in \$str1 ($str1) using regex with * quantifier"; } my $str2 = "2 dog:cat"; if ($str2 =~ /(cat)/ ) { my $capture = $1; say "matched |$capture| in \$str2 ($str2) using regex without quan +tifier"; }else{ say "no match in \$str2 ($str2) using regex without quantifier"; } say "-" x10; my $str3 = "3 cat:dog" =~ /(cat)/; say "\$str3: $str3"; # 1 -- original replaced by scal +ar value my $str4 = "4 cat:dog"; if ($str4 =~ /(cat)/ ) { my $capture = $1; say "matched |$capture| in \$str4 ($str4) using regex without quan +tifier"; }else{ say "fubar on |$str4| using regex without quantifier"; } =head $str0 1 $str1: 1 dog:cat Use of uninitialized value $capture in concatenation (.) or string at +F:\_wo\junk20111109.pl line 11. matched || in $str1 (1 dog:cat) using * quantifier in regex matched |cat| in $str2 (2 dog:cat) using regex without quantifier ---------- $str3: 1 matched |cat| in $str4 (4 cat:dog) using regex without quantifier =cut
(This in no way deprecates choroba's discussion but is offered in the hope that the simpler example here may be more accessible).

In reply to Re^5: Likely trivial regex question by ww
in thread Likely trivial regex question by moodywoody

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.