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 |