in reply to regexp for searching incremental expressions
As tomte suggests, you can achieve this using deferred evaluation; the trick is to take advantage of the special variable $^N, which refers to "the most recently closed capture" in the match so far.
Using this, we get for example:
% perl -le 'print $& if shift =~ /(\d)(((??{$^N + 1})))+/' 4321234 1234 %
This works because the first time through the eval we have seen (for example) the digit '1', so we return '2' as the next thing to match; if we match it, that match is itself captured and becomes the last thing captured, so the next time into the eval $^N is '2' and we return '3' as the new thing to match.
Note firstly that $^N was introduced in perl-5.8.0, so you won't be able to use this with earlier versions; secondly, enabling warnings gives the unexpected complaint:
.. which looks like a bug, and you can safely ignore that warning in this case.(((??{$^N + 1})))+ matches null string many times before HERE mark in +regex m/((\d)(((??{$^N + 1})))+ << HERE )/ at -e line 1.
Hugo
|
|---|