<?xml version="1.0" encoding="windows-1252"?>
<node id="640613" title="Re: Arbitrary number of captures in a regular expression" created="2007-09-23 16:59:55" updated="2007-09-23 12:59:55">
<type id="11">
note</type>
<author id="106949">
Sidhekin</author>
<data>
<field name="doctext">
&lt;p&gt; ... and if you still want a one-step approach ... &lt;/p&gt;

&lt;p&gt;This must be a job for the /g modifier and its side-kick, \G: &lt;/p&gt;

&lt;c&gt;
my (@match) = $str =~ /(?:^foo |(?&lt;!^)\G)m (\d+) (?=(?:m \d+ )*bar)/g;
&lt;/c&gt;

&lt;p&gt; (The negative lookbehind in order to prevent matching strings starting with the &lt;c&gt;m \d+ &lt;/c&gt; pattern &amp;mdash; the positive lookahead to prevent matching strings that don't properly close with &lt;c&gt;bar&lt;/c&gt;.) &lt;/p&gt;

&lt;p&gt; With a little test case, it looks like this: &lt;/p&gt;

&lt;c&gt;
my @test = ( 'foo m 1 m 2 m 3 m 4 bar',
             'foo m 2 m 4 m 7 bar',
             'foo m 1 bar',
             'm 2 foo m 1 bar',
             'foo m 1 c 2 bar',
             'foo m 1 bar m 2',
             'foo m 1 m 5 m 7',
           );

for my $str (@test) {
  my (@match) = $str =~ /(?:^foo |(?&lt;!^)\G)m (\d+) (?=(?:m \d+ )*bar)/g;
  local $" = ', ';
  print "'$str' =&gt; (@match)\n";
}
&lt;/c&gt;

&lt;p&gt; ... and outputs like this: &lt;/p&gt;

&lt;c&gt;
'foo m 1 m 2 m 3 m 4 bar' =&gt; (1, 2, 3, 4)
'foo m 2 m 4 m 7 bar' =&gt; (2, 4, 7)
'foo m 1 bar' =&gt; (1)
'm 2 foo m 1 bar' =&gt; ()
'foo m 1 c 2 bar' =&gt; ()
'foo m 1 bar m 2' =&gt; (1)
'foo m 1 m 5 m 7' =&gt; ()&lt;/c&gt;

&lt;p&gt; &lt;b&gt;Update:&lt;/b&gt; Almost missed the "bar" requirement.  Fixed now, right? &lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-106949"&gt;
&lt;p align=center&gt;
&lt;font size=-1&gt;&lt;code&gt;print "Just another Perl ${\(trickster and hacker)},"&lt;/code&gt;&lt;/font&gt;
&lt;br&gt;The Sidhekin &lt;b&gt;proves&lt;/b&gt; Sidhe did it!&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
640610</field>
<field name="parent_node">
640610</field>
</data>
</node>
