in reply to Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression


It's already solved, but while we're at it how about a but of fun? ; >

This snippet pulls out the data beforehand and then processes it later:

my @data = $text =~ /foo(.*?)bar/g; manipulate($_) foreach (@data);
This could be very memory intensive if you have a large $text however (since it will probably generate all the entries before it builds the array). On the other hand, you only have to use the RE once, which should at least somewhat speed things up (unless you're keen on precompiling your RE's anyway : )

HTH,
jynx


Update: You can also shorten this into:

manipulate($_) for (@{[$text =~ /foo(.*?)bar/g]});
But that might be a little overboard...
  • Comment on Re: Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression
  • Select or Download Code