eibwen has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Global Regex sans //g
by tlm (Prior) on May 02, 2005 at 03:13 UTC | |
by eibwen (Friar) on May 02, 2005 at 05:03 UTC | |
by tlm (Prior) on May 02, 2005 at 09:26 UTC | |
by ysth (Canon) on May 02, 2005 at 12:38 UTC | |
|
Re: Global Regex sans //g
by Errto (Vicar) on May 02, 2005 at 02:53 UTC |