Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by Polyglot (Friar)
on May 26, 2021 at 14:45 UTC ( #11133071=note: print w/replies, xml ) Need Help??


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

LanX,

Your code does a better job at meeting my understanding. It's still on the edge to have that $^N involved, but that is the sort of method I was hoping to learn. I'll need to use it more to get the feel of what it is doing, perhaps, but I can see that your code does do the job.

...and you found a typo in my OP. Nobody's perfect. My forearms and fingers are actually sore from typing for a solid 7-8 hours both yesterday and today, so I guess I'll lay the blame there (lame excuse, perhaps, but true nonetheless).

Blessings,

~Polyglot~

  • Comment on Re^4: Regexp substitution on variable-length ranges with embedded code? ( $^N )
  • Download Code

Replies are listed 'Best First'.
Re^5: Regexp substitution on variable-length ranges with embedded code? (??{...})
by LanX (Sage) on May 26, 2021 at 15:59 UTC
    > 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://11133071]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-09-17 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?