![]() |
Replacing spaces
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/ /\ \;/g; $string =~ s/([A-Za-z])\ \;([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+/\ \;/; 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. |
Re: Replacing spaces
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/ /\ \;/g; > $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g; > I'm just learning these kind of things, but you can replace those two with $string =~ s/^([ ](?{ $cnt .= "\ \;"}))*/$cnt/; Worked with the spaces .. Awaiting critiques from the regulars .. :) Abhinav |
Re: Replacing spaces
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 |
Re: Replacing spaces
Aristotle <qwerty97654@hotmail.com> 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/ /\ \;/g; > $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g; > This should work 1 while ( $string =~ s/^\s/\ /g ); Eberhard |
Re: Replacing spaces
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> 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. > 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 |
Re: Replacing spaces
Jürgen Exner <jurgenex@hotmail.com> wrote:
>> 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 Sorry, shame on me! You are right I was WRONG, I've wrong tested. Eberhard |
Re: Replacing spaces
Eberhard Niendorf wrote:
> Aristotle <qwerty97654@hotmail.com> 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/ /\ \;/g; >> $string =~ s/([A-Za-z])\ \;([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 |
Re: Replacing spaces
Abhinav <matrix_calling@yahoo.dot.com> wrote in comp.lang.perl.misc:
> 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/ /\ \;/g; > > $string =~ s/([A-Za-z])\ \;([A-Za-z])/$1 $2/g; > > > > I'm just learning these kind of things, but you can replace those two with > > $string =~ s/^([ ](?{ $cnt .= "\ \;"}))*/$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 " "s would collect in it. Anno |
Re: Replacing spaces
Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
> 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; Here's another: / */g; substr( $_, 0, pos) =~ s/ / /g; Anno |
Re: Replacing spaces
Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
> 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; Here's another: / */g; substr( $_, 0, pos) =~ s/ / /g; Anno |
| All times are GMT. The time now is 07:14 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.