分为上限和数字

时间:2012-12-05 16:30:24

标签: regex perl

如何在大写字母和数字上拆分变量看起来像这样

$Var = "HvacSystem8000Series";

需要的结果是:

$Var = "Hvac System 8000 Series";

代码:

$Var =~ s/^[A-Z]/ /igs;

2 个答案:

答案 0 :(得分:6)

评论中的解释:

$Var = "HvacSystem8000Series";
$Var =~ s/(?<=[[:lower:]])   # Low case before,
          (?=[[:upper:]0-9]) # upper case or digit after;
          |                  # OR
          (?<=[0-9])         # digit before,
          (?=[[:upper:]])    # upper case after?
          /\ /xg;            # Replace with a space.

答案 1 :(得分:0)

最简单的方法是收集您认为是单词的列表,然后将它们与空格分隔符连接在一起。

use strict;
use warnings;

my $var = "HvacSystem8000Series";
$var = join ' ', $var =~ /[A-Z]+[a-z]*|\d+/g;

print $var

<强>输出

Hvac System 8000 Series