http://www.perlmonks.org?node_id=432727

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

Wise ones,

I'm a newbie to perl and I need some help with regular expressions to do some string manipulation.
The first thing I want to be able to do is add a space where there are capital letters, except right at the beginning of a sentence. At first I thought split would work, but this removes the capital letter. This is basically what I want to achieve:

HelloMyNameIsJake = Hello My Name Is Jake

The second thing I want to do is reduce all whitespace to a single space char. So any number of single spaces, tabs or newlines would become just " ".

I would be grateful if anyone out there can help me with this and perhaps give me a break down of what you have done so I understand it for the future.

Many thanks, Jake

  • Comment on addd spaces after caps & reduce whitespace to single space

Replies are listed 'Best First'.
Re: addd spaces after caps & reduce whitespace to single space
by esskar (Deacon) on Feb 19, 2005 at 15:28 UTC
    1:
    my $text = "HelloMyNameIsJake"; $text =~ s!(.)([A-Z])!$1 $2!g; print "[$text]";
    2:
    my $text = "So much spaces,\t\t\t\t\t\ttabs and\n\n\n\n\n\n\ +n newlines"; $text =~ s!\s+! !g; print $text;
Re: addd spaces after caps & reduce whitespace to single space
by ambrus (Abbot) on Feb 19, 2005 at 15:38 UTC
Re: addd spaces after caps & reduce whitespace to single space
by betterworld (Curate) on Feb 19, 2005 at 16:18 UTC
    How about this:
    my $s="HelloMyNameIsJake AndThisIsTooMuchSpace"; my @a= $s =~ m/([A-Z][a-z]*)/g; print "@a";
    Note that this treats special characters and spaces alike.
Re: addd spaces after caps & reduce whitespace to single space
by sh1tn (Priest) on Feb 19, 2005 at 15:33 UTC
    Capital letters to white spaces:
    $string =~ s/([A-Z])/ $1/g;
    All whitespaces to a single space char:
    $string =~ s/(\s+)/ /gm;


      Sht1tn OP has asked that white space is not allowed at the beginning of the sentence.

      Also he asked more that one spaces or tabs or newlines.

      So your answers wont work for the above conditions.

      update:when you are updating your node, use update in your node.

      Prasad

        IMHO - no need for $1 and $2 capturing:
        $string =~ s/(?<=.)([A-Z])/ $1/g;


          A reply falls below the community's threshold of quality. You may see it by logging in.