Replacing spaces

Discussion in 'Perl Misc' started by Aristotle, Sep 19, 2004.

  1. Aristotle

    Aristotle Guest

    I'm trying to replace spaces at the beggining of a string, with
    " " .
    Not all spaces by a single   , but rather each space by a single
    " "

    eg
    " make love not war" --> "    make love not
    war"
    " follow the white rabbit" --> "  follow the white rabbit"

    ie " " should replace only the beggining spaces (one by one), but
    not other spaces.

    The way i'm doing this for now is

    $string =~ s/ /\&nbsp\;/g;
    $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;

    ie, first replacing all spaces with   , then replacing again
    those between two words. It gets the job somewhat done (a bit
    inefficiently, since if there are other characters within the string
    (like ",.-:;" ) the " " arent being replaced).

    If i try to use $string =~ s/^\s+/\&nbsp\;/; then all beggining spaces
    are being replaced by a single " ", while what i need is the
    number of " " to match the number of spaces at the beggining of
    the string.

    I'd appreciate your help on this.

    Thank you in advance.
     
    Aristotle, Sep 19, 2004
    #1
    1. Advertising

  2. Aristotle

    Abhinav Guest

    Aristotle wrote:
    > I'm trying to replace spaces at the beggining of a string, with
    > " " .
    > Not all spaces by a single   , but rather each space by a single
    > " "
    >
    > eg
    > " make love not war" --> "    make love not
    > war"
    > " follow the white rabbit" --> "  follow the white rabbit"
    >
    > ie " " should replace only the beggining spaces (one by one), but
    > not other spaces.
    >
    > The way i'm doing this for now is
    >
    > $string =~ s/ /\&nbsp\;/g;
    > $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
    >


    I'm just learning these kind of things, but you can replace those two with

    $string =~ s/^([ ](?{ $cnt .= "\&nbsp\;"}))*/$cnt/;

    Worked with the spaces ..

    Awaiting critiques from the regulars .. :)


    Abhinav
     
    Abhinav, Sep 19, 2004
    #2
    1. Advertising

  3. Aristotle wrote:
    > I'm trying to replace spaces at the beggining of a string, with
    > " " .
    > Not all spaces by a single   , but rather each space by a single
    > " "
    >
    > eg
    > " make love not war" --> "    make love not
    > war"
    > " follow the white rabbit" --> "  follow the white rabbit"
    >
    > ie " " should replace only the beggining spaces (one by one), but
    > not other spaces.


    One way to do it:

    s/^(\s*)/' ' x length $1/e;

    jue
     
    Jürgen Exner, Sep 19, 2004
    #3
  4. Aristotle <> wrote:

    > I'm trying to replace spaces at the beggining of a string, with
    > "&nbsp;" .
    > Not all spaces by a single &nbsp; , but rather each space by a single
    > "&nbsp;"
    >
    > eg
    > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    > war"
    > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    >
    > ie "&nbsp;" should replace only the beggining spaces (one by one), but
    > not other spaces.
    >
    > The way i'm doing this for now is
    >
    > $string =~ s/ /\&nbsp\;/g;
    > $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
    >


    This should work

    1 while ( $string =~ s/^\s/\&nbsp;/g );

    Eberhard
     
    Eberhard Niendorf, Sep 19, 2004
    #4
  5. Eberhard Niendorf wrote:
    > Aristotle <> wrote:
    >
    >> I'm trying to replace spaces at the beggining of a string, with
    >> "&nbsp;" .
    >> Not all spaces by a single &nbsp; , but rather each space by a single
    >> "&nbsp;"
    >>
    >> eg
    >> " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    >> war"
    >> " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    >>
    >> ie "&nbsp;" should replace only the beggining spaces (one by one),
    >> but not other spaces.


    > This should work
    >
    > 1 while ( $string =~ s/^\s/\&nbsp;/g );


    Why didn't you test it?
    Your while loop succeeds exactly once, then there is no leading space any
    longer and all remaining spaces will, well, remain.

    jue
     
    Jürgen Exner, Sep 19, 2004
    #5
  6. Jürgen Exner <> wrote:

    >> This should work
    >>
    >> 1 while ( $string =~ s/^\s/\&nbsp;/g );

    >
    > Why didn't you test it?
    > Your while loop succeeds exactly once, then there is no leading space any
    > longer and all remaining spaces will, well, remain.
    >
    > jue


    Sorry, shame on me! You are right I was WRONG, I've wrong tested.

    Eberhard
     
    Eberhard Niendorf, Sep 19, 2004
    #6
  7. Aristotle

    Abhinav Guest

    Eberhard Niendorf wrote:
    > Aristotle <> wrote:
    >
    >
    >>I'm trying to replace spaces at the beggining of a string, with
    >>"&nbsp;" .
    >>Not all spaces by a single &nbsp; , but rather each space by a single
    >>"&nbsp;"
    >>
    >>eg
    >>" make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    >>war"
    >>" follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    >>
    >>ie "&nbsp;" should replace only the beggining spaces (one by one), but
    >>not other spaces.
    >>
    >>The way i'm doing this for now is
    >>
    >> $string =~ s/ /\&nbsp\;/g;
    >> $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
    >>

    >
    >
    > This should work
    >
    > 1 while ( $string =~ s/^\s/\&nbsp;/g );
    >


    I tried this before brewing that complex concoction up-thread .. It will
    not work as the match succeeds only once ..

    Of course, Jurgen's solution is better than mine :)

    Regards

    Abhinav
     
    Abhinav, Sep 19, 2004
    #7
  8. Aristotle

    Anno Siegel Guest

    Abhinav <> wrote in comp.lang.perl.misc:
    > Aristotle wrote:
    > > I'm trying to replace spaces at the beggining of a string, with
    > > "&nbsp;" .
    > > Not all spaces by a single &nbsp; , but rather each space by a single
    > > "&nbsp;"
    > >
    > > eg
    > > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    > > war"
    > > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    > >
    > > ie "&nbsp;" should replace only the beggining spaces (one by one), but
    > > not other spaces.
    > >
    > > The way i'm doing this for now is
    > >
    > > $string =~ s/ /\&nbsp\;/g;
    > > $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
    > >

    >
    > I'm just learning these kind of things, but you can replace those two with
    >
    > $string =~ s/^([ ](?{ $cnt .= "\&nbsp\;"}))*/$cnt/;
    >
    > Worked with the spaces ..
    >
    > Awaiting critiques from the regulars .. :)


    Well, it doesn't run under strictures. Also, $cnt should be cleared
    before each call, otherwise "&nbsp"s would collect in it.

    Anno
     
    Anno Siegel, Sep 20, 2004
    #8
  9. Aristotle

    Anno Siegel Guest

    Jürgen Exner <> wrote in comp.lang.perl.misc:
    > Aristotle wrote:
    > > I'm trying to replace spaces at the beggining of a string, with
    > > "&nbsp;" .
    > > Not all spaces by a single &nbsp; , but rather each space by a single
    > > "&nbsp;"
    > >
    > > eg
    > > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    > > war"
    > > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    > >
    > > ie "&nbsp;" should replace only the beggining spaces (one by one), but
    > > not other spaces.

    >
    > One way to do it:
    >
    > s/^(\s*)/'&nbsp;' x length $1/e;


    Here's another:

    / */g;
    substr( $_, 0, pos) =~ s/ /&nbsp/g;

    Anno
     
    Anno Siegel, Sep 20, 2004
    #9
  10. Aristotle

    Anno Siegel Guest

    Jürgen Exner <> wrote in comp.lang.perl.misc:
    > Aristotle wrote:
    > > I'm trying to replace spaces at the beggining of a string, with
    > > "&nbsp;" .
    > > Not all spaces by a single &nbsp; , but rather each space by a single
    > > "&nbsp;"
    > >
    > > eg
    > > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    > > war"
    > > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    > >
    > > ie "&nbsp;" should replace only the beggining spaces (one by one), but
    > > not other spaces.

    >
    > One way to do it:
    >
    > s/^(\s*)/'&nbsp;' x length $1/e;


    Here's another:

    / */g;
    substr( $_, 0, pos) =~ s/ /&nbsp;/g;

    Anno
     
    Anno Siegel, Sep 20, 2004
    #10
  11. Aristotle

    Ian Wilson Guest

    OT: Re: Replacing spaces

    Aristotle wrote:

    > I'm trying to replace spaces at the beggining of a string, with
    > "&nbsp;" .
    > Not all spaces by a single &nbsp; , but rather each space by a single
    > "&nbsp;"
    >
    > eg
    > " make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
    > war"
    > " follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
    >
    > ie "&nbsp;" should replace only the beggining spaces (one by one), but
    > not other spaces.
    >
    > The way i'm doing this for now is
    >
    > $string =~ s/ /\&nbsp\;/g;
    > $string =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
    >
    > ie, first replacing all spaces with &nbsp; , then replacing again
    > those between two words. It gets the job somewhat done (a bit
    > inefficiently, since if there are other characters within the string
    > (like ",.-:;" ) the "&nbsp;" arent being replaced).
    >
    > If i try to use $string =~ s/^\s+/\&nbsp\;/; then all beggining spaces
    > are being replaced by a single "&nbsp;", while what i need is the
    > number of "&nbsp;" to match the number of spaces at the beggining of
    > the string.
    >
    > I'd appreciate your help on this.
    >
    > Thank you in advance.


    I'd be using <pre> or <blockquote>, probably with some CSS. The
    non-breaking space isn't intended for indentation and, to me, looks an
    ugly way of achieving that.

    YMMV :)
     
    Ian Wilson, Sep 20, 2004
    #11
    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. John B. Matthews
    Replies:
    4
    Views:
    684
    John B. Matthews
    Sep 12, 2008
  2. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    299
    Chris Hohmann
    Apr 11, 2005
  3. Vapor ..

    replacing spaces

    Vapor .., Nov 19, 2007, in forum: Ruby
    Replies:
    2
    Views:
    100
    Vapor ..
    Nov 19, 2007
  4. George George

    replacing verticle white spaces?

    George George, Jul 17, 2009, in forum: Ruby
    Replies:
    4
    Views:
    154
  5. Philo Hippo

    problem replacing spaces in a string

    Philo Hippo, Oct 11, 2004, in forum: Javascript
    Replies:
    2
    Views:
    87
    Philo Hippo
    Oct 12, 2004
Loading...

Share This Page