in reply to Splitting on uppercase letters

The secret is in the regular expression you use to split upon. I'm thinking zero-width positive look-ahead assertion but that's just me. Documentation is available at perlre and perlretut. If you're just starting out, perhaps you should check out perlrequick for an overview. But to answer your question:

#!/usr/bin/perl -wl $,=$"; print split/(?=[A-Z])/,"ThisString"; print split/(?=[A-Z])/,"SomeOtherString"; __DATA__ output: This String Some Other String

Hope this helps.

Updated: Just realized that I forgot to set $,. If not set as it is above, then the output would not have had spaces.

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

Replies are listed 'Best First'.
Re: Re: Splitting on uppercase letters
by davido (Cardinal) on Sep 10, 2003 at 20:16 UTC
    antirice's advice is a great way of doing it. But there's only one problem. The original post suggested that the outcome from "ThisString" should be "this" and "string" (note the upper case delimeters have become lower case).

    Not wanting to abandon antirice's efficient advice, I would suggest just doing this:

    print map lc split/(?=[A-Z])/,"ThisString"; print map lc split/(?=[A-Z])/,"SomeOtherString"; __DATA__ output: this string some other string

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein