You are partially right, split can consume the characters that it uses to split on but it is not always the case. Consider the following two code snippets

$ perl -e ' > $str = q{abcXghiXstu}; > @elems = split m{X}, $str; > print qq{$_\n} for @elems;' abc ghi stu $ perl -e ' > $str = q{abcXghiXstu}; > @elems = split m{(X)}, $str; > print qq{$_\n} for @elems;' abc X ghi X stu $

As you can see, the capturing parentheses in the regular expression of the second snippet cause split to keep the separators and assign them to the output array. So split consumes the characters only if you let it.

This behaviour does not help us with the lower/upper case split but you can see from this post that you can split on a zero-width match, or, in other words, a boundary condition. In the following snippet I want to split on the boundary between letters and digits so I use zero-width look behind and look-ahead assertions to match a point in the string preceded by a letter and followed by a digit or vice versa.

$ perl -e ' > $str = q{abc123def456ghi}; > @elems = > split m > {(?x) > (?: > (?<=[a-z]) # look behind for letter > (?=[0-9]) # look ahead for digit > ) > | # or > (?: > (?<=[0-9]) # look behind for digit > (?=[a-z]) # look ahead for letter > ) > }, $str; > print qq{$_\n} for @elems;' abc 123 def 456 ghi $

I hope this throws more light on the various ways split can be used.

Cheers,

JohnGG


In reply to Re^2: Using punctuation to split a text by johngg
in thread Using punctuation to split a text by UrbanHick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.