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

Re: What is wrong here (Replacing Strin pieces)

by kcott (Canon)
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 making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-02-24 03:07 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (351 votes). Check out past polls.