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

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

by Anonymous Monk
on May 26, 2021 at 12:46 UTC ( #11133058=note: print w/replies, xml ) Need Help??


in reply to Regexp substitution on variable-length ranges with embedded code?

Use repeats and $^N example in https://metacpan.org/pod/perlretut
  • Comment on Re: Regexp substitution on variable-length ranges with embedded code? ( $^N )

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

      > 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~

Log In?
Username:
Password:

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

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



    Results (93 votes). Check out past polls.

    Notices?