Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Replacing spaces

Reply
Thread Tools

Replacing spaces

 
 
Aristotle
Guest
Posts: n/a
 
      09-19-2004
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.
 
Reply With Quote
 
 
 
 
Abhinav
Guest
Posts: n/a
 
      09-19-2004
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-19-2004
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


 
Reply With Quote
 
Eberhard Niendorf
Guest
Posts: n/a
 
      09-19-2004
Aristotle <(E-Mail Removed)> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-19-2004
Eberhard Niendorf wrote:
> Aristotle <(E-Mail Removed)> 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


 
Reply With Quote
 
Eberhard Niendorf
Guest
Posts: n/a
 
      09-19-2004
Jürgen Exner <(E-Mail Removed)> 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
 
Reply With Quote
 
Abhinav
Guest
Posts: n/a
 
      09-19-2004
Eberhard Niendorf wrote:
> Aristotle <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2004
Abhinav <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2004
Jürgen Exner <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-20-2004
Jürgen Exner <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments