如何在字母字符和数字字符之间插入空格?
这是我原来问题的延续: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将第一个替换更改为:
另请参阅 perldoc perlre。
Change the first substitution to be:
See also perldoc perlre.
添加以下两个替换:
第一个替换在数字和字母之间的任何过渡处插入空格。末尾的
i
修饰符使表达式不区分大小写。第二个则相反。Add the following two substitutions:
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.从昨晚开始,一个 5.10+ 的解决方案,
使用 tchrist 让我重新发现的 \p{} 属性[1]: )这基本上与上次的解决方案相同,但现在我们也检查数字。
1: http://perldoc.perl.org/perluniprops.html#Properties -可通过-\p{}-和-\P{}访问
Continuing from last night, a 5.10+ solution,
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{}