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

Discussion in 'Perl Misc' started by rsarpi, May 13, 2007.

  1. rsarpi

    rsarpi Guest

    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
     
    rsarpi, May 13, 2007
    #1
    1. Advertising

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

    rsarpi wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, May 13, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    5
    Views:
    2,159
    jongalloway
    Jan 16, 2005
  2. TTroy
    Replies:
    16
    Views:
    815
    Peter Nilsson
    Jan 31, 2005
  3. John B. Matthews
    Replies:
    4
    Views:
    697
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    658
  5. johkar
    Replies:
    2
    Views:
    3,047
    Mayeul
    Dec 10, 2009
Loading...

Share This Page