Search code examples
perlcgicgi-bin

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

Solution

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