Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

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


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

> $^N

indeed

use v5.12; use warnings; 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'; say $str; $str =~ s#(\d+:\d+:)(\d);(?: \1((??{$^N+1}));)+#$1$2-$3#g; say $str;

C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/sequencer.pl 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 43:1:1-6 27:3:7-9 65:1:4; 65:1:18 Compilation finished at Wed May 26 15:46:31

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

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

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

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-05-20 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?