$1 is a dynamically scoped variable that contains details from the last successful match in the current scope. It is NOT set to undef after a failed match.

Regarding your comments to ikegami, they dont seem so relevent here. I didnt use an eval operator, as for how to accomplish the goal with the conditional pattern and not alternation, well ive already done that havent I? If you mean regarding MRE, id say just work through the book. There is a way of thinking about things that makes regexes easy to understand, and there is a way that will lead to confusion. I dont recall specifically anything that gives a particularly good discussion of the conditional pattern. I have never encountered one in live code.

---
$world=~s/war/peace/g


In reply to Re^5: Regex conditional match if previous match in same expression is true? by demerphq
in thread Regex conditional match if previous match in same expression is true? by radiantmatrix

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.