sashac88 has asked for the wisdom of the Perl Monks concerning the following question:

I have to write names of the files in some file.Here are examples of file names:
BoundariesSwitchGARP.txt
CliCreateConfFile.txt
BoundariesSystemIPAddressing.txt
BoundariesSystemDiagnostics.txt
Mib2cliQosSet.txt

Each file has to be written as following:
whenever I see one capital letter (unless it appears in beginning of the file name)I should add a space before it.
So for example:
CliCreateConfFile.txt will be written:
Cli Create Conf File.txt
BoundariesSystemDiagnostics.txt will be written:
Boundaries System Diagnostics.txt
My problem is that there can be a different amount of Capitall letters.How can I trace all cases in one rule.
Thanks you very much in advance.

20040705 Edit by Corion: Changed title from 'Hey ,Monks.'

Replies are listed 'Best First'.
Re: Add spaces before capital letters
by pelagic (Priest) on Jul 05, 2004 at 07:09 UTC
    use strict; my @files = qw[BoundariesSwitchGARP.txt CliCreateConfFile.txt BoundariesSystemIPAddressing.txt BoundariesSystemDiagnostics.txt Mib2cliQosSet.txt ]; foreach my $file (@files) { print "$file\t"; $file =~ s/(\B[A-Z])/ $1/g; print "$file\n"; } __OUTPUT__ BoundariesSwitchGARP.txt Boundaries Switch G A R P.txt CliCreateConfFile.txt Cli Create Conf File.txt BoundariesSystemIPAddressing.txt Boundaries System I P Addressing.tx +t BoundariesSystemDiagnostics.txt Boundaries System Diagnostics.txt Mib2cliQosSet.txt Mib2cli Qos Set.txt
    update to show before and after.

    pelagic
Re: Add spaces before capital letters
by BUU (Prior) on Jul 05, 2004 at 07:07 UTC
    Use the s/// operator. Search for a capital letter: [A-Z], capture it, then replace with the capture and a space. Use the "begin of line" anchor with a negated look behind to make sure the capital isn't beginning the line. In other words:
    s/(?<!^)([A-Z])/ $1/g