Split on capital letters and numbers
문제
I need this script to split on capital letters and numbers. I've got the split on capital letters part working but I can't seem to figure out the number side of it.
Needed result: 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";
해결책
A few s///
transformations will give you what you need:
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";
다른 팁
I suggest you just use a regular expression match to find all the required "words" within the string and then join them with spaces. This program demonstrates. It counts /
as a word, so these can just be substituted for double colons to complete the process
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;
output
Hvac System 8000 Series :: Heating System 8000 Series :: Boilers
Like pilcrow's answer but, you know, different
#!/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";
제휴하지 않습니다 StackOverflow