The best I can come up with is to use look-arounds for the
spliting and then
splice to concatenate single capitals. It is not perfect as you can see from the third data item.
use strict;
use warnings;
my @strings = (
q{ThisIsACamelCasedString},
q{AStringWithFTP},
q{HeIsANASAAstronaut},
);
my $rxCamel = qr
{(?x)
(?<=[a-z])(?=[A-Z])
|
(?<=[A-Z])(?=[A-Z])
};
foreach my $string ( @strings )
{
print qq{String: $string\n};
my @words = split m{$rxCamel}, $string;
for my $idx ( reverse 1 .. $#words )
{
if
(
$words[$idx] =~ m{^[A-Z]+$}
and
$words[$idx - 1] =~ m{^[A-Z]$}
)
{
$words[$idx - 1] .= splice @words, $idx, 1;
}
}
print qq{ $_\n} for @words;
}
Here's the output.
String: ThisIsACamelCasedString
This
Is
A
Camel
Cased
String
String: AStringWithFTP
A
String
With
FTP
String: HeIsANASAAstronaut
He
Is
ANASA
Astronaut
I think there will be too many corner cases for this task to be be achieved without some comparison with perhaps a dictionary list of acronyms.
I hope this is of use.
Cheers,
JohnGG
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.