Actually you don't really need $^N, since you can test wether $3 is even defined. It makes the code also more robust against the insertion of other match-groups.
The real power tool is the insertion of dynamically evaluated code with (??{... }) and I have to admit that I rarely need this.
use v5.12; use warnings; use Test::More; my $str = '43:1:1; 43:1:2; 43:1:3; 43:1:4; 43:1:5; 43:1:6; 27:3:7; 27: +3:8; 27:3:9; 65:1:4; 65:1:18'; my $exp = '43:1:1-6; 27:3:7-9; 65:1:4; 65:1:18'; my $got = $str; $got =~ s( ( \d+:\d+: ) # $1 ( \d+ ) # $2 (?: # repeat ;\s # separator \1 # head ( (??{ ($3//$2) + 1 }) # insert 1+ ($3 or $2) ) # $3 )+ # once or more )($1$2-$3)xg; is( $got, $exp, "fits"); done_testing;
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
In reply to Re^5: Regexp substitution on variable-length ranges with embedded code? (??{...})
by LanX
in thread Regexp substitution on variable-length ranges with embedded code?
by Polyglot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |