Ever since I've read Obfuscated regexp, I've been trying to figure out how to create a global regex using (?{ }) as opposed to m//g. I've spent awhile looking at the code and refractoring it, but I still can't seem to figure out why this iterates:

qr/<([\w\d]+)\s+(?{ print $^N })\s*?>/;

I've passed the code through use re qw/debug/; and YAPE::Regex::Explain, which rewrote the regex:

(?-imsx:<([\w\d]+)\s+(?{ print $^N })\s*?>)

Yet while I (believe I) understand how the first match is made, I still cannot seem to figure out why subsequent matches occur.

I've tried creating simplified regexes to illustrate the idiom:

#!/usr/bin/perl -w use strict; my $s = "abacada"; # 'bcd' delimited by 'a' print $s =~ /[^a]/g; # prints 'bcd' $s =~ /([^a])(?{ print $^N })/; # prints 'b'

However I cannot seem to duplicate the functionality with a terse example as my attempts have only returned the first match (as use re qw/debug/; confirms). While the reason for this behavior is fairly obvious given the regex, it underscores the fact that I have yet to grasp how the first code iterates.

Can someone please elucidate what I'm missing so that I might progress along the path of enlightment?


In reply to Global Regex sans //g by eibwen

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.