2012-10-10 74 views
0

我需要此脚本拆分大写字母和数字。我有大写字母部分的分裂工作,但我似乎无法弄清楚它的数字方面。拆分大写字母和数字

所需结果:Hvac System 8000 Series :: Heating System 8000 Series :: Boilers

#!/usr/bin/perl 

print "Content-type: text/html\n\n"; 

use CGI qw(:standard); 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 
use strict; 

my $Last_URL = "HvacSystem8000Series/HeatingSystem8000Series/Boilers"; 


my ($Cat,$Sub1,$Sub2) = split(/\//, $Last_URL, 3); 

if ($Sub2) { 

    $Last_URL = "<b>$Cat :: $Sub1 :: $Sub2</b>"; 
} 
else { 

    $Last_URL = "<b>$Cat :: $Sub1</b>"; 
} 

my @Last_URL = $Last_URL =~ s/(.)([A-Z][^A-Z][^0-9])/$1 $2/g; 
print "$Last_URL"; 

回答

1

几个s///变换会给你你需要的东西:

for ($Last_URL) { 
    s/ ([a-z]) ([A-Z0-9])/"$1 $2" /egx; # Foo123 -> Foo 123 
    s/ ([0-9]) ([A-Z])/"$1 $2" /egx;  # 123Bar -> 123 Bar 
    s!/! " :: " !egx;     # / -> " :: " 
} 
print $Last_URL, "\n"; 
0

我建议你只使用正则表达式匹配找到所有需要的“话”在字符串内,然后用空格将它们连接起来。这个程序演示。据统计/的话,那么这刚好可以取代双冒号来完成此过程

use strict; 
use warnings; 

my $Last_URL = "HvacSystem8000Series/HeatingSystem8000Series/Boilers"; 

(my $string = join ' ', $Last_URL =~ m<[A-Z][a-z]*|\d+|/>g) =~ s|/|::|g; 

print $string; 

输出

Hvac System 8000 Series :: Heating System 8000 Series :: Boilers 
0

像段落符号的答案,但是,你要知道,不同的

#!/usr/bin/env perl 

use strict; 
use warnings; 

my $string = "HvacSystem8000Series/HeatingSystem8000Series/Boilers"; 

$string =~ s/(?<=\p{Ll})(?=\p{Lu}|\pN)/ /g; 
$string =~ s/(?<=\pN)(?=\p{Lu})/ /g; 
$string =~ s'/' :: 'g; 

print "$string\n"; 
相关问题