【发布时间】:2015-04-22 21:36:54
【问题描述】:
我有一个类似的字符串:
SomeCamel WasEnteringText
我已经找到了使用 php str_replace 拆分字符串和插入空格的各种方法,但是我在 perl 中需要它。
有时字符串前可能有一个空格,有时没有。有时字符串中会有空格,但有时没有。
我试过了:
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;
还有更多 =~s//g 的组合;经过大量阅读。
我需要一位大师来引导这头骆驼走向答案的绿洲。
好的,根据下面的输入,我现在有了:
$camel =~ s/([A-Z])/ $1/g;
$camel =~ s/^ //; # Strip out starting whitespace
$camel =~ s/([^[:space:]]+)/\u$1/g;
它完成了它,但似乎过分了。虽然有效。
【问题讨论】:
-
P.S.我希望结果是“Some Camel Was Entering Text”