Isn't $1 supposed to contain the thing I matched?

update: The answer is: pattern first (DEFINE) last

#!/usr/bin/perl -- use strict; use warnings; use 5.010; use Data::Dump; my $re = qr{ (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d ) ) # match this ( # save to $1 \b (?&byte) ( \. (?&byte) ){3} \b ) }x; my $str = ' localhost 127.66.66.66 '; while( $str =~ m/$re/g ){ dd( [ $1, \%+, \%-, \%/ ] );#/ } dd([ $str =~ m/$re/g ] ); use Tie::Hash::NamedCapture; tie my %hash, "Tie::Hash::NamedCapture", all => 1; if( $str =~ m/$re/g ){ dd([ $1, \%hash ]); } __END__ [ undef, { # tied Tie::Hash::NamedCapture }, { # tied Tie::Hash::NamedCapture byte => [undef], }, {}, ] [undef, "127.66.66.66", ".66"] [ undef, { # tied Tie::Hash::NamedCapture byte => [undef], }, ]

update: The answer is: pattern first (DEFINE) last

so if I use

my $re = qr{ # match this ( # save to $1 \b (?&byte) ( \. (?&byte) ){3} \b ) (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d ) ) }x;
then $1 is as expected

In reply to $1 empty with (?(DEFINE)) and (?&NAME) by Anonymous Monk

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.