Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl: the Markov chain saw
 
PerlMonks  

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,

loris

Comment on Splitting on non-initial uppercase without split
Select or Download Code
Re: Splitting on non-initial uppercase without split
by si_lence (Deacon) on Jan 26, 2005 at 10:22 UTC
    Hi
    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;

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

    si_lence

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

      --
      edan

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;
    --
    edan

      How about:
      $s =~ s/([a-z])([A-Z])/$1 $2/ig;
      (untested)
        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;

        si_lence

      What about this:

      $s =~ s/\B(?=[[:upper:]])/ /g;
      or this
      $s =~ s/\B([[:upper:]])/ $1/g;
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, $/;
    gives
    This Is The House That Jack Built
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (15)
As of 2014-04-16 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (432 votes), past polls