Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Replace all characters inbetween

by jwkrahn (Abbot)
on Jun 05, 2022 at 12:35 UTC ( [id://11144411]=note: print w/replies, xml ) Need Help??


in reply to Replace all characters inbetween

$ perl -le' my $x = "AAAAAXXXXXXXAAAXXXXXAXXXXAAAA"; print $x; $x =~ s/(?<=A)X+(?=A)/ "A" x ( $+[0] - $-[0] ) /eg; print $x; ' AAAAAXXXXXXXAAAXXXXXAXXXXAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Or:

$ perl -le' my $x = "AAAAAXXXXXXXAAAXXXXXAXXXXAAAA"; print $x; $x =~ s/(?<=A)X+(?=A)/@{[ "A" x ( $+[0] - $-[0] ) ]}/g; print $x; ' AAAAAXXXXXXXAAAXXXXXAXXXXAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Replies are listed 'Best First'.
Re^2: Replace all characters inbetween
by Anonymous Monk on Jun 05, 2022 at 12:48 UTC
    Thank you! How about the begin and end of the string? I realised that it does not change these, if present.

      Do you mean something like this?

      Win8 Strawberry 5.8.9.5 (32) Sun 06/05/2022 10:21:09 C:\@Work\Perl\monks >perl use strict; use warnings; for my $s ( 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA', 'AXA', 'XXXXXXXAAAXXXXXAXXXX', 'XXXXX', 'XA', 'AX', 'X', 'AAAAA', 'YXY', 'A', '', ) { (my $t = $s) =~ s{ (?<! [^A]) X+ (?! [^A]) } {@{[ 'a' x ($+[0] - $-[0]) ]}}xmsg; print "'$s' \n'$t' \n\n"; } ^Z 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA' 'AAAAAaaaaaaaAAAaaaaaAaaaaAAAA' 'AXA' 'AaA' 'XXXXXXXAAAXXXXXAXXXX' 'aaaaaaaAAAaaaaaAaaaa' 'XXXXX' 'aaaaa' 'XA' 'aA' 'AX' 'Aa' 'X' 'a' 'AAAAA' 'AAAAA' 'YXY' 'YXY' 'A' 'A' '' ''


      Give a man a fish:  <%-{-{-{-<

      What is supposed to happen at the begin and end of the string? Please at least give some examples.


      Give a man a fish:  <%-{-{-{-<

      How about the begin and end of the string? I realised that it does not change these, if present.

      the problem statement was substitute all occurences of a character (let's call it 'X') that is inbetween two others

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found