Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
"be consistent"
 
PerlMonks  

Re: What is wrong here (Replacing Strin pieces)

by kcott (Abbot)
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


Comment on Re: What is wrong here (Replacing Strin pieces)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-04-18 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls