Monks,
I may be overlooking something, but as far as I can tell, given the following strings (simplified from a real-world example, but sufficient for the purpose):
foo m 1 m 2 m 3 m 4 bar foo m 2 m 4 m 7 bar foo m 1 bar
There may be any number of m digit sequences following 'foo'. You cannot group and capture an arbitrary number of times in a single pattern and get back all the captures? That is, the following does not work:
my (@match) = ($str =~ /^foo (?:m (\d+) )+bar/);
That only captures the final digit group. I think the only way out is to use a pile of code and do something like
my @match; if ($str =~ /^foo /) { while ($str =~ /m (\d+) /g) { push @match, $1; } }
There must be a simpler way, but I can't see it right now, worse, that solution doesn't strike me as being particularly well-anchored, that is, following 'foo'.
The chain of m digit+ could be anywhere in string, for instance, after 'bar', and it would be incorrect to match and capture them. Thanks for any suggestions and insights you may have.
• another intruder with the mooring in the heart of the Perl
In reply to Arbitrary number of captures in a regular expression by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |