Replacing spaces

A

Aristotle

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.
 
A

Abhinav

Aristotle said:
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
 
J

Jürgen Exner

Aristotle said:
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
 
E

Eberhard Niendorf

Aristotle said:
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;

This should work

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

Eberhard
 
J

Jürgen Exner

This should work

1 while ( $string =~ s/^\s/\ /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
 
E

Eberhard Niendorf

Jürgen Exner said:
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
 
A

Abhinav

Eberhard said:
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;


This should work

1 while ( $string =~ s/^\s/\ /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
 
A

Anno Siegel

Abhinav said:
Aristotle said:
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 .. :)

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

Anno
 
I

Ian Wilson

Aristotle said:
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.

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 :)
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top