Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Splitting on non-initial uppercase without split

by loris (Hermit)
on Jan 26, 2005 at 10:15 UTC ( #425160=perlquestion: print w/replies, xml ) Need Help??
loris has asked for the wisdom of the Perl Monks concerning the following question:

Hello RegExpers,

I know form the answers to Split: Having your capitals and not eating them that how to split a run-together word LikeThis using:

my @w = split(/(?=[A-Z])/,$s);

but can I do something similar using:

$s =~ s/(?=[A-Z])/ /g; $s =~ s/^\s//g;

in a single line?

Thanks for your help,


Replies are listed 'Best First'.
Re: Splitting on non-initial uppercase without split
by edan (Curate) on Jan 26, 2005 at 10:46 UTC

    This works, but I have the distinct feeling there is a more straightforward way to do it...

    $s =~ s/(?<!^)(?=[A-Z])/ /g;

      What about this:

      $s =~ s/\B(?=[[:upper:]])/ /g;
      or this
      $s =~ s/\B([[:upper:]])/ $1/g;
      How about:
      $s =~ s/([a-z])([A-Z])/$1 $2/ig;
        I guess you would want to make your regex case sensitive:
        $s =~ s/([a-z])([A-Z])/$1 $2/g;

        But then I think it still would not fit the description as it
        woud not split something like "Test1Test2" into "Test1 Test2"
        So following your line of thought it should be something like
        $s =~ s/([^A-Z])([A-Z])/$1 $2/g;

Re: Splitting on non-initial uppercase without split
by si_lence (Deacon) on Jan 26, 2005 at 10:22 UTC
    If I get you right you want to have a blank before each
    capital letter except the first one in the string. Right?
    If yes use
    $s =~ s/.(?=[A-Z])/ /g;

    The comment of edan is right (sorry to mess it up)
    but this should work
    $s =~ s/(.)(?=[A-Z])/$1 /g;


      Erm, no. That's going to turn "LikeThis" into "Lik This"


Re: Splitting on non-initial uppercase without split
by dimar (Curate) on Jan 26, 2005 at 20:15 UTC

    Since no one else has mentioned it yet, here is an example that uses the SlashG anchor. (SlashG is typed like that just to make it search-engine-distinguishable).

    $_ = "ThisIsACamelCaseWorld"; my @aTemp; @aTemp = m/\G[A-Z]?[a-z]*/g; ## same thing print join " ", @aTemp; __END__ This Is A Camel Case World
Re: Splitting on non-initial uppercase without split
by Anonymous Monk on Jan 26, 2005 at 12:31 UTC

      Yep. Or per the OP's substitution, which I prefer:

      substr( $s, 1 ) =~ s/(?=[A-Z])/ /g;

      Makeshifts last the longest.

Re: Splitting on non-initial uppercase without split
by ambrus (Abbot) on Jan 26, 2005 at 14:52 UTC

    I don't quite undrestand your question, but

    $w = "ThisIsTheHouseThatJackBuilt"; $w = join " ", $w =~ /[[:upper:]][ +^[:upper:]]*/g; print $w, $/;
    This Is The House That Jack Built

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://425160]
Approved by Mutant
[Corion]: Maybe you could reduce ITIL to a computer network, a man and a dog. The network runs the computations. The man is there to feed the dog. The dog is there to prevent the man from accessing the computers.
[marto]: from experience of this side of the fence, it's all about proving to people that you are ITIL compliant, then the client ignores logical processes anyway :P
[marto]: when you work on the outside, for a client, you can make them aware of the stupids, but they don' t always listen :/
[Corion]: Yeah, you can lead them to water, but it's illegal to drown them...
[Discipulus]: Here my solution marto: create a win fake machine insied AD. do a micro fake partition foreach remote partition you wont to monit. put a web server that acept from data from remote systems and full fake partition as needed. fake partition are Mb. ;=)
[marto]: Discipulus yeah, the nuclear industry love fake machines sitting around on a controlled domain :P
[marto]: changing a typo on a website is often a great deal of paper work :P

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-01-24 10:37 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.