Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Regexp substitution on variable-length ranges with embedded code? (??{...})

by LanX (Sage)
on May 26, 2021 at 15:59 UTC ( #11133079=note: print w/replies, xml ) Need Help??


in reply to Re^4: Regexp substitution on variable-length ranges with embedded code? ( $^N )
in thread Regexp substitution on variable-length ranges with embedded code?

> It's still on the edge to have that $^N involved

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2022-05-26 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?