Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: What is wrong here (Replacing Strin pieces)

by kcott (Chancellor)
on Jul 17, 2013 at 07:23 UTC ( #1044743=note: print w/replies, xml ) Need Help??

in reply to What is wrong here (Replacing Strin pieces)

Rather than getting bogged down with alternating between the start and end strings and then doing a repeat substitution, just capture the middle bit and tag a '?' on either end. This works with the sample string you provided:

$ perl -Mstrict -Mwarnings -E ' my $string = "---------xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy-------- +"; $string =~ s/^-*(.*?)-*$/?$1?/; say $string; ' ?xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy?

Update: Actually, that may not be what you want at all!

I read:

  • "replace all characters ..." [plural] "... with another character" [singular]
  • "The output is: ?xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy?" [wanted output]
  • "replace ever - ..." [plural] "... with an ?" [singular]

So, assuming I've completely misinterpreted your question, I'd recommend ++rjt's solution. However, if you still wanted something without alternations or repeating, you could use this:

$ perl -Mstrict -Mwarnings -E ' my $string = "---------xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy-------- +"; $string =~ s/^(-*)(.*?)(-*)$/"?" x length($1) . $2 . "?" x length( +$3)/e; say $string; ' ?????????xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy????????

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044743]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-04-20 14:51 GMT
Find Nodes?
    Voting Booth?