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

Hello RegexpSplitters,

Is there a quick way to split a word with internal capitals at those capitals and to retain them? Doing

my @w = split(/[A-Z]/,$s);

obviously turns

anUnwieldyVariableName

into an array with

an nwieldy ariable ame

which is not what I want.

Any ideas?

loris

Replies are listed 'Best First'.
Re: Split: Having your capitals and not eating them
by edan (Curate) on Nov 11, 2004 at 12:49 UTC
Re: Split: Having your capitals and not eating them
by grinder (Bishop) on Nov 11, 2004 at 13:24 UTC

    Capturing what you split upon will return the split boundaries:

    perl -le 'print $_ for split( /([A-Z])/, shift)' AbcDefGhi A bc D ef G hi

    Note that edan's solution does something slightly different:

    % perl -le 'print $_ for split( /(?=[A-Z])/, shift )' AbcDefGhi Abc Def Ghi

    Choose your poison :)

    - another intruder with the mooring of the heat of the Perl

      It is probably the former, given the post.....

      print join ' | ', split /(?=[A-Z])/, "thisIsCamelCase"; __DATA__ this | Is | Camel | Case

        I think you mean "the latter." ;-)

        --
        edan