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:
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
In reply to Re: What is wrong here (Replacing Strin pieces)
by kcott
in thread What is wrong here (Replacing Strin pieces)
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |