Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Replacing multiple spaces into one character?

by r.joseph (Hermit)
on Oct 09, 2001 at 07:02 UTC ( #117623=perlquestion: print w/ replies, xml ) Need Help??
r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. Suppose the string:  . . .Perlmonks  . . .is . .the . . . .coolest .place. Notice how each dot respresents a space (I did that for clarity, so that you could visualize multiple spaces (including leading spaces) - just realize that there can be any number of space between words).

I want to take the above string and turn it into: Perlmonks_is_the_coolest_place

Basically, replacing any number of spaces between words with ONE underscore. I tried:
$str =~ s/\s+/_/ig;
but that replaces each space with an underscore. Removing the "g" modifier from the regex does (obviously) no good.

I realize that this will probably end up being a very simple solution, but I have been studying all day and my brain is fried, so please help a poor, brain-fried college student out? Thanks all!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Comment on Replacing multiple spaces into one character?
Select or Download Code
Re: Replacing multiple spaces into one character?
by John M. Dlugosz (Monsignor) on Oct 09, 2001 at 07:10 UTC
    Er, it works for me. To wit:
    $str= " perlmonks is the coolest"; $str =~ s/\s+/_/g; print $str;
    prints: _perlmonks_is_the_coolest

    (I left out the i modifier because space is the same in capital or lower-case. I don't think that would change the behavior, though!)

    —John

      Agreed, it works exactly as r.joseph presented it. However you may want to think about using
      $str =~ s/ +/_/g;
      instead, since it doesn't mess with your newline characters. That is, with your version "perl\n" becomes "perl_".

      - Boldra
Re: Replacing multiple spaces into one character?
by chromatic (Archbishop) on Oct 09, 2001 at 07:10 UTC
    tr/ /_/s;, as /s is for squash multiple.

    Doing it in one line while removing leading whitespace is trickier. There are several potentially nasty regular expressions. Just say no.

    Update: Squashing several expressions onto one line technically counts, but it wasn't what I meant, smart guy!

      $str = ' say as simple as a b c :-) '; $str =~ s/^\s+|\s+$//g, $str =~ tr/ /_/s, print $str;
Re: Replacing multiple spaces into one character?
by tachyon (Chancellor) on Oct 09, 2001 at 07:16 UTC

    Not recommended but TIMTOWTDI requires someone post this ;-)

    $str =' Perlmonks is a very cool place '; $str = join '_', split /\s+/, $str; print $str;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      you could also use the magical ' ' (single space) arg to split, which will remove leading spaces as well...
      #!/usr/bin/perl -wT use strict; my $str =' Perlmonks is a very cool place '; $str = join '_', split ' ', $str; print $str, "\n"; =OUTPUT Perlmonks_is_a_very_cool_place

      -Blake

        Thanks blakem that null string that you get using /\s+/is a pain. I never realised that ' ' was magical.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Replacing multiple spaces into one character?
by Zaxo (Archbishop) on Oct 09, 2001 at 07:36 UTC

    This uses the magical whitespace arg to split:

    my $str = " Perlmonks is the\tcoolest\n \tplace"; $str = join "_", split " ", $str; print $str,$/;
    The magic strips leading whitespace and is greedy about the rest. This is a little broader than you asked for, but still does what you want.

    After Compline,
    Zaxo

Re: Replacing multiple spaces into one character?
by pike (Monk) on Oct 09, 2001 at 11:22 UTC
    How about:

    $str = ' xx y z '; $str =~ s/(?<=\w)\s+(?=\w)/g; #replace only spaces between letters $str=~ tr/ //d; #delete leading and trailing spaces

    pike

Re: Replacing multiple spaces into one character?
by thunders (Priest) on Oct 09, 2001 at 19:01 UTC
    1 while $str =~ s/[ ]+/_/;
    will also work for this one, it's similar to the /g modifier except that it re-evaluates the entire string after every substitution. But this is for entertainment TIMTOWTDI value only cause something like this
    1 while $str =~ s/[ ]+/ _ /;
    will just keep running forever as it tries to replace the spaces it creates.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://117623]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (16)
As of 2014-07-25 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls