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";
4

3 回答 3

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";
于 2012-10-10T18:28:23.927 回答
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
于 2012-10-10T19:59:54.503 回答
0

就像 pilcrow 的回答,但是,你知道,不同

#!/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";
于 2012-10-10T23:57:39.967 回答