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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |