如何在字母字符和数字字符之间插入空格?

发布于 2024-10-07 21:26:27 字数 808 浏览 7 评论 0原文

这是我原来问题的延续: Perl-如何在除第一次出现或现有的每个大写字母之前插入空格?

我能够实现在字符串中仅包含字母的大写字母之间插入空格的所需结果

my $camel ="SomeCamel IsEnteringText";
$camel =~ s/(?<=[a-z])(?=[A-Z])/ /g;
$camel =~ s/([^[:space:]]+)/\u$1/g;

一些骆驼正在输入文本

但是,当存在数字时,我遇到了问题:

my $camel ="Some 440Camel220 IsEntering100Text Nogo";
$camel =~ s/(?<=[a-z])(?=[A-Z])/ /g;
$camel =~ s/([^[:space:]]+)/\u$1/g;

打印:

大约 440Camel220 正在输入 100Text Nogo

所需:

一些 440 Camel 220 正在输入 100 个文本 Nogo

那么,我现在如何在字母和数字之间插入空格?

This is a continuation of my original question:
Perl- How do I insert a space before each capital letter except for the first occurrence or existing?

I was able to achieve a desired result of inserting spaces between caps with only letters in the string:

my $camel ="SomeCamel IsEnteringText";
$camel =~ s/(?<=[a-z])(?=[A-Z])/ /g;
$camel =~ s/([^[:space:]]+)/\u$1/g;

Prints:

Some Camel Is Entering Text

But, when numbers are present, I ran into issues:

my $camel ="Some 440Camel220 IsEntering100Text Nogo";
$camel =~ s/(?<=[a-z])(?=[A-Z])/ /g;
$camel =~ s/([^[:space:]]+)/\u$1/g;

Prints:

Some 440Camel220 Is Entering100Text Nogo

Desired:

Some 440 Camel 220 Is Entering 100 Text Nogo

So, how do I now insert a space between the letters and the numbers?.

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

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

发布评论

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

评论(3

白云悠悠 2024-10-14 21:26:27

将第一个替换更改为:

$camel =~ s/(?<=[a-z])(?=[A-Z0-9])|(?<=[0-9])(?=[A-Z])/ /g;

另请参阅 perldoc perlre

Change the first substitution to be:

$camel =~ s/(?<=[a-z])(?=[A-Z0-9])|(?<=[0-9])(?=[A-Z])/ /g;

See also perldoc perlre.

甜警司 2024-10-14 21:26:27

添加以下两个替换:

$camel =~ s/(?<=[0-9])(?=[a-z])/ /ig;
$camel =~ s/(?<=[a-z])(?=[0-9])/ /ig;

第一个替换在数字和字母之间的任何过渡处插入空格。末尾的 i 修饰符使表达式不区分大小写。第二个则相反。

Add the following two substitutions:

$camel =~ s/(?<=[0-9])(?=[a-z])/ /ig;
$camel =~ s/(?<=[a-z])(?=[0-9])/ /ig;

The first inserts a space at any transition between a digit and a letter. The i modifier at the end makes the expression case-insensitive. The second does the opposite.

绅士风度i 2024-10-14 21:26:27

从昨晚开始,一个 5.10+ 的解决方案,

$camel =~ s/(?:\p{Lu}\p{Ll}*+|[0-9]++)(?!\W)\K/ /g;

使用 tchrist 让我重新发现的 \p{} 属性[1]: )这基本上与上次的解决方案相同,但现在我们也检查数字。

1: http://perldoc.perl.org/perluniprops.html#Properties -可通过-\p{}-和-\P{}访问

Continuing from last night, a 5.10+ solution,

$camel =~ s/(?:\p{Lu}\p{Ll}*+|[0-9]++)(?!\W)\K/ /g;

With the \p{} properties[1] that tchrist had me rediscover : ) It's basically the same solution as last time, but now we check for numbers too.

1: http://perldoc.perl.org/perluniprops.html#Properties-accessible-through-\p{}-and-\P{}

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