
regexp: saving backreferences that won't overwrite existing values
You could change how you group expressions
#!/usr/bin/perl
$_="test: begin foo bar bat baz end";
/begin(?:\s(\w+))*\send/;
print "$1\n"; # prints 'baz'
/begin((?:\s\w+)*)\send/;
print "$1\n"; # prints ' foo bar bat baz'
Quote:
> If I do something like
> /begin(?:\s(\w+))*\send/
> I'll have last word matched at $1. Any way to make them "pile" up so
> that I'll get everything that was at $1 before it got overwritten?
> I tried like the following:
> but it saved all the failed matches too. Of course, I guess this was
> because my construct had to be backtracked (engine matching "end" at
> first, then realizing it doesn't work), and I guess it would have worked
> with some proper zero-width assertions, but still the idea shouldn't
> change; that there should be a way to save everything the "(\w+)" _did_
> match in the end.
> How?
> P.S. I realize that there were other ways to do the example above; it's
> merely to demonstrate the feature I'd like in the regexp. To do in
> single regexp, you see? I'm sure I could've come up with better example
> that didn't solve with some splitting and/or loops, but ...
> -Kaatunut