Perl-如何在每个大写字母之前插入空格(第一次出现或现有的除外)?

发布于 2024-10-07 14:13:24 字数 651 浏览 9 评论 0原文

我有一个像这样的字符串:

 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;

这可以完成但似乎过多。不过有效。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

因为看清所以看轻 2024-10-14 14:13:24
s/(?<!^)[A-Z][a-z]*+(?!\s+)\K/ /g;

还有更少的“拧这个胡言乱语”的版本:

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.

编辑:我注意到,当你在混合中添加标点符号时,这也会增加额外的空白,这可能不是OP想要的;值得庆幸的是,解决方法只是将负面展望从 \s+ 更改为 \W+。尽管现在我开始想知道为什么我实际上添加了这些优点。德拉茨,我!

EDIT2:呃,抱歉,最初忘记了 /g 标志。

EDIT3:好吧,有人否决了我。我变得迟钝了。不需要对 ^ 进行消极的回顾 - 我真的在这个问题上失败了。希望修复:

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

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

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

喵星人汪星人 2024-10-14 14:13:24

尝试:

$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

请注意 $camel =~ s/([^ ])([AZ])/$1 $2/g; 的明显尝试有一个错误:如果有大写字母依次排列(例如“ABCD”将转换为“ABCD”而不是“ABC D”)

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")

鹊巢 2024-10-14 14:13:24

尝试 :
s/(?<=[az])(?=[AZ])/ /g

这将在小写字符之后(即不是空格或字符串开头)和大写字符之前插入空格。

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.

甜味超标? 2024-10-14 14:13:24

改进……

Hughmeir 上,这也适用于以小写字母开头的数字和单词。

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

测试

 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文