http://qs1969.pair.com?node_id=11138614


in reply to Re^3: s///g within m//g regions
in thread s///g within m//g regions

# turn * into - within |...| regions
A |simple*example| is |so*simple| to come*up |with
these*days|
should become
# turn * into - within |...| regions
A |simple-example| is |so-simple| to come*up |with
these-days|
So, iterate over matches, and perform substs within each match.

Replies are listed 'Best First'.
Re^5: s///g within m//g regions
by hippo (Bishop) on Nov 09, 2021 at 13:50 UTC
    use strict; use warnings; use feature 'state'; use Test::More tests => 1; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days|'; my $want = 'A |simple-example| is |so-simple| to come*up |with these-days|'; my $have = join '|', map { state $i = 0; $i++ % 2 and s/\*/-/g; $_ } split /\|/, $in, -1; is $have, $want;

    🦛

Re^5: s///g within m//g regions (updated)
by LanX (Saint) on Nov 09, 2021 at 14:11 UTC
    that's very different because there are no lines in between.

    use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got = $in; $got =~ s[ ($start) (.*?) ($stop) ][ "$1" . ( $2 =~ tr/\*/-/r ) . "$3" ]gsxe; is($got,$exp); done_testing;

    updates
    added
    • test
    • line-break

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

Re^5: s///g within m//g regions
by LanX (Saint) on Nov 09, 2021 at 20:30 UTC
    TIMTOWTDI

    use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got; for ( split /($start|$stop)/s, $in) { if ( /$start/.../$stop/ ) { #print "<$_>"; s/\*/-/gs; } $got .= $_; } is($got,$exp); done_testing;

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

      > TIMTOWTDI

      use strict; use warnings; use Test::More; my $in = 'A |simple*example| is |so*simple| to come*up |with these*days| according to some *experts*'; my $exp = 'A |simple-example| is |so-simple| to come*up |with these-days| according to some *experts*'; my $start = qr/\|/; my $stop = qr/\|/; my $got = join "", map { /$start/.../$stop/ ? s/\*/-/gr : $_ } split /($start|$stop)/s, $in; is($got,$exp); done_testing;

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