Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by LanX (Saint)
on May 26, 2021 at 14:32 UTC ( [id://11133069]=note: print w/replies, xml ) Need Help??


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

> PS: beautifying with /x modifier and adding missing ; in replacement left for the interested reader. :)

well since my editor has a (mostly functioning) cperl-beautify-regexp

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+ # (updated +) ) # $2 = first $^N ; (?: # group w/o match \s \1 ( (??{ $^N+ 1 }) # include last_match + 1 ) # $3 = next $^N ; )+ # repeat /$1$2-$3;/xg; is($got, $exp, "fits"); done_testing;

Worth noting that the OP was wrong with his expectation, it's 43:1:1-6; not 43:1:1-5;

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

PS: still not perfect, because the input isn't terminated with a semicolon, but I'm tired now. :)

Replies are listed 'Best First'.
Re^4: Regexp substitution on variable-length ranges with embedded code? ( $^N )
by Polyglot (Chaplain) on May 26, 2021 at 14:45 UTC
    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~

      > 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://11133069]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found