habhab has asked for the wisdom of the Perl Monks concerning the following question:

I have a string of form:
"From:        Some Text"
#number of spaces is arbitrary
#length of Some Text is arbitrary

I need this string to look like this
"From:Some Text"

Thanks,

Replies are listed 'Best First'.
Re: How can I strip an arbitary number of "inner" spaces from a string?
by Aristotle (Chancellor) on Sep 17, 2004 at 00:00 UTC

    Your example implies that sequences of more than one space should be deleted completely. Is that correct? That would be

    s/ {2,}//g;

    You provided too little specification and too little sample data, though. (More of either would have been better.) You're likely to get 10 different answers each guessing at a slightly different approach to solve your problem. One of them is probably going to be what you need, and everyone else just wasted their time. Please read How (Not) To Ask A Question and be more precise next time.

    Makeshifts last the longest.

Re: How can I strip an arbitary number of "inner" spaces from a string?
by ikegami (Patriarch) on Sep 17, 2004 at 00:00 UTC
    Is s/^(From:)\s+/$1/; good enough? or maybe s/^([^:]+:)\s+/$1/;?

    Update: Added missing ':'

      ITYM s/^([^:]+:)\s+/$1/;

        A bit shorter but should still work okay:
        s/(\:)\s+/$1/
        or just for fun:
        s/(:) +/$1/
        Second probably has loads of reasons why it'll fall over in other cases but seems to work fine here.
Re: How can I strip an arbitary number of "inner" spaces from a string?
by Kraegar (Novice) on Sep 17, 2004 at 20:32 UTC
    Perhaps I'm missing something here, but wouldn't it be as simple as:

    s/\s\s+/ /g

    - Krae

Re: How can I strip an arbitary number of "inner" spaces from a string?
by sidj (Initiate) on Sep 17, 2004 at 06:49 UTC
    $string="From: Some Text";
    $string=~s/\s+//;

    \s+ will match one or more occurances of white space
    $string =~/\s+//g ;
    ## global operator g will strip all the white spaces present

      That will remove not just runs of multiple blanks, but all blanks in his string.

      Makeshifts last the longest.