in reply to Re: RegExp Matched Values
in thread RegExp Matched Values

$string =~ m/^(frm)(\w+?)(txt)?$/;

There's a problem here in that if  'txt' may not be present (as  (txt)? implies) and an overall match is still desired,  $3 will be undefined because the third capture group need not match at all. A different approach will yield an empty string instead.

>perl -wMstrict -le "for (@ARGV) { m/^(frm)(\w+?)(txt)?$/; print qq{'$_' ($1) ($2) ($3)}; } print '-----------'; for (@ARGV) { m/^(frm)(\w+?)(txt|)$/; print qq{'$_' ($1) ($2) ($3)}; } " frmNametxt frmNameFoo 'frmNametxt' (frm) (Name) (txt) Use of uninitialized value $3 in concatenation (.) or string at ... 'frmNameFoo' (frm) (NameFoo) () ----------- 'frmNametxt' (frm) (Name) (txt) 'frmNameFoo' (frm) (NameFoo) ()