#Attempt to capture 2 patterns by combining above regex's $newvar =~ /(?:#augment:A)(.*)(?:\n)(?:#augment:B)(.*)(?:\n)/ ; say $1; say $2; #Does not work - nothing is captured and I get uninitialized value $1 and $2 warnings...

You've been shown why this is not matching as you want, but please also consider that $1 and $2 are only uninitialized because you haven't previously successfully captured a match in your program. If you had, those variables would still contain the matches they captured earlier.

From perlre:

NOTE: Failed matches in Perl do not reset the match variables, which m +akes it easier to write code that tests for a series of more specific + cases and remembers the best match.

Thus it's important to check whether the match succeeded before using $1:

#!/usr/bin/perl use strict; use warnings; my $status = "it's a beautiful day but I am feeling crapy"; # note typ +o $status =~ /(beautiful|average|tough)/; print "Day: $1\n"; $status =~ /(happy|alright|crappy)/; print "Feeling: $1\n"; __END__
The above code does not give the expected results:
[22:08][nick:~/monks]$ perl 1139246.pl Day: beautiful Feeling: beautiful [22:08][nick:~/monks]$
Add a test and we avoid the bug:
#!/usr/bin/perl use strict; use warnings; my $status = "it's a beautiful day but I am feeling crapy"; # note typ +o if ( $status =~ /(beautiful|average|tough)/ ) { print "Day: $1\n"; } else { print "No match for day\n"; } if ( $status =~ /(happy|alright|crappy)/ ) { print "Feeling: $1\n"; } else { print "No match for feeling\n"; } __END__
Output:
[22:13][nick:~/monks]$ perl 1139246.pl Day: beautiful No match for feeling [22:13][nick:~/monks]$

The way forward always starts with a minimal test.

In reply to Re: regex capture group problem by 1nickt
in thread regex capture group problem by midiperl

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.