2012-10-10 75 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"; 
相關問題