<?xml version="1.0" encoding="windows-1252"?>
<node id="1004231" title="Re: Capturing all instances of a repeating sub-pattern in regex" created="2012-11-16 13:41:56" updated="2012-11-16 13:41:56">
<type id="11">
note</type>
<author id="712372">
kennethk</author>
<data>
<field name="doctext">
&lt;p&gt;It seems odd that you are doing all this in a substitution rather than iterating over the results of the match, &lt;em&gt;e.g.&lt;/em&gt; &lt;c&gt;while ($text =~ /$re/g) {...}&lt;/c&gt;, particularly given that you are already resorting to some complex activity including an &lt;c&gt;e&lt;/c&gt; modifier.  However, you could do this in a fell swoop by capturing the entire block you want to reparse, and using a sub-regex in list context w/ the g modifier to return the full list:
&lt;c&gt;    $text =~ s/
          (.+?)            (?:\s*\n)+
      ((?: \d+ (?:\sPSI)? (?:\s*\n)+ ){4})
     /push @r, [ $1, $2 =~ m|\d+ (?:\sPSI)?|xg ]/esgx;
&lt;/c&gt;
&lt;p&gt;If I were the maintenance guy that followed you, I would not think friendly thoughts when I saw that.  
&lt;p&gt;You could get something a little more maintainable by explicitly expanding your &lt;c&gt;{4}&lt;/c&gt; terms:
&lt;c&gt;    $text =~ s/
          (.+?)            (?:\s*\n)+
      (?: (\d+ (?:\sPSI)?) (?:\s*\n)+ )
      (?: (\d+ (?:\sPSI)?) (?:\s*\n)+ )
      (?: (\d+ (?:\sPSI)?) (?:\s*\n)+ )
      (?: (\d+ (?:\sPSI)?) (?:\s*\n)+ )
     /push @r, [ $1, $2, $3, $4, $5 ]/esgx;
&lt;/c&gt;
which could be refactored into
&lt;c&gt;    my $sub_re = qr/ (\d+ (?:\sPSI)?) (?:\s*\n)+ /x;
    $text =~ s/
          (.+?)            (?:\s*\n)+
      $sub_re
      $sub_re
      $sub_re
      $sub_re
     /push @r, [ $1, $2, $3, $4, $5 ]/esgx;
&lt;/c&gt;
&lt;p&gt;None of this changes the fact that you have a fundamental obfuscation by using a substitution instead of a loop.

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-712372"&gt;
&lt;hr /&gt;
&lt;p&gt;#11929 First ask yourself `How would I do this without a computer?'  Then have the computer do it the same way.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1004225</field>
<field name="parent_node">
1004225</field>
</data>
</node>
