in reply to How to split CamelCase?
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
|
|---|