How can I search and replace a string while preserving (not removing) trailing spaces?

R

rsarpi

Sorry the dumb question. I'm a newbie.

But how can I search and replace a string while preserving (not
removing) trailing spaces?

let me explain: the command 's/$old_string/$new_string/g' inserts the
new string and *moves* spaces to the right or left (depending on the
length of the 'new_string') making the whole sentence bigger or
smaller.

for example:

my $sentence = "*Peter Parker is Spider Man *";
print length($sentence); #prints 43 characters

I'd like to know a trick in which, if I get rid of "Peter" I would
_still_ get 43 characters in length for the whole sentence between
both quotation marks. Here the whole sentence between quotation marks
expands until it reaches 43 chars in length.

my $sentence = "*Parker is Spider Man *";
print length($sentence );#still 43


And if I add to the sentence Benjamin (between Peter and Parker) I
would like to get exactly 43 characters between both quotation marks.
Here the whole sentence between quotation marks shrinks until it gets
up 43 chars in length.

my $sentence = "*Peter Benjamin Parker is Spider Man *";
print length($sentence ); #still 43
 
J

John W. Krahn

rsarpi said:
Sorry the dumb question. I'm a newbie.

But how can I search and replace a string while preserving (not
removing) trailing spaces?

let me explain: the command 's/$old_string/$new_string/g' inserts the
new string and *moves* spaces to the right or left (depending on the
length of the 'new_string') making the whole sentence bigger or
smaller.

for example:

my $sentence = "*Peter Parker is Spider Man *";
print length($sentence); #prints 43 characters

I'd like to know a trick in which, if I get rid of "Peter" I would
_still_ get 43 characters in length for the whole sentence between
both quotation marks. Here the whole sentence between quotation marks
expands until it reaches 43 chars in length.

my $sentence = "*Parker is Spider Man *";
print length($sentence );#still 43


And if I add to the sentence Benjamin (between Peter and Parker) I
would like to get exactly 43 characters between both quotation marks.
Here the whole sentence between quotation marks shrinks until it gets
up 43 chars in length.

my $sentence = "*Peter Benjamin Parker is Spider Man *";
print length($sentence ); #still 43

$ perl -e'
my $sentence = "Peter Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
$sentence = "Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
$sentence = "Peter Benjamin Parker is Spider Man";
printf "*%-43.43s*\n", $sentence;
'
*Peter Parker is Spider Man *
*Parker is Spider Man *
*Peter Benjamin Parker is Spider Man *


perldoc -f printf
perldoc -f sprintf
perldoc -f pack
perldoc -f unpack



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top