Perl- How do I insert a space before each capital

2019-07-19 10:24发布

I have a string like:

 SomeCamel WasEnteringText

I have found various means of splitting up the string and inserting spaces with php str_replace but, i need it in perl.

Sometimes there may be a space before the string, sometimes not. Sometimes there will be a space in the string but, sometimes not.

I tried:

    my $camel = "SomeCamel WasEnteringText";
    #or
    my $camel = " SomeCamel WasEntering Text";
    $camel =~ s/^[A-Z]/\s[A-Z]/g;
    #and
    $camel =~ s/([\w']+)/\u$1/g;

and many more combinations of =~s//g; after much reading.

I need a guru to direct this camel towards an oasis of answers.

OK, based on the input below I now have:

$camel =~ s/([A-Z])/ $1/g;
$camel =~ s/^ //; # Strip out starting whitespace
$camel =~ s/([^[:space:]]+)/\u$1/g;

Which gets it done but seems excessive. Works though.

标签: perl space
4条回答
等我变得足够好
2楼-- · 2019-07-19 10:44

Try:

$camel =~ s/(?<! )([A-Z])/ $1/g; # Search for "(?<!pattern)" in perldoc perlre 
$camel =~ s/^ (?=[A-Z])//; # Strip out extra starting whitespace followed by A-Z

Please note that the obvious try of $camel =~ s/([^ ])([A-Z])/$1 $2/g; has a bug: it doesn't work if there are capital letters following one another (e.g. "ABCD" will be transformed into "ABCD" and not "A B C D")

查看更多
你好瞎i
3楼-- · 2019-07-19 10:49

Try : s/(?<=[a-z])(?=[A-Z])/ /g

This inserts as space after a lower case character (ie not a space or start of string) and before and upper case character.

查看更多
forever°为你锁心
4楼-- · 2019-07-19 10:53
s/(?<!^)[A-Z][a-z]*+(?!\s+)\K/ /g;

And the less "screw this horsecrap" version:

s/
 (?<!^)          #Something not following the start of line,
    [A-Z][a-z]*+ #That starts with a capital letter and is followed by
                 #Zero or more lowercased letters, not giving anything back,
 (?!\s+)          #Not followed by one or more spaces,
\K               #Better explained here [1]
/ /gx;            #"Replace" it with a space.

EDIT: I noticed that this also adds extra whitespace when you add punctuation into the mix, which probably isn't what the OP wants; thankfully, the fix is simply changing the negative look ahead from \s+ to \W+. Although now I'm beginning to wonder why I actually added those pluses. Drats, me!

EDIT2: Erm, apologies, originally forgot the /g flag.

EDIT3: Okay, someone downvote me. I went retarded. No need for the negative lookbehind for ^ - I really dropped the ball on this one. Hopefully fixed:

s/[A-Z][a-z]*+(?!\W)\K/ /gx;

1: http://perldoc.perl.org/perlre.html

查看更多
不美不萌又怎样
5楼-- · 2019-07-19 11:04

Improving ...

... on Hughmeir's, this works also with numbers and words starting with lower-case letters.

s/[a-z0-9]+(?=[A-Z])\K/ /gx

Tests

 myBrainIsBleeding     => my_Brain_Is_Bleeding
 MyBrainIsBleeding     => My_Brain_Is_Bleeding
 myBRAInIsBLLEding     => my_BRAIn_Is_BLLEding
 MYBrainIsB0leeding    => MYBrain_Is_B0leeding
 0My0BrainIs0Bleeding0 => 0_My0_Brain_Is0_Bleeding0
查看更多
登录 后发表回答