in reply to Using punctuation to split a text

In any case, would
@sents=split(/([.?!]|[:lower:]\u)/,$text);
work?

I think the split function consumes the characters it splits on though, right, so you'd have to use $1 and append it to the previous record, then dealing with the lower/upper boundary would be even more problematic.

_________________________________________________________________________________

I like computer programming because it's like Legos for the mind.

Replies are listed 'Best First'.
Re^2: Using punctuation to split a text
by johngg (Canon) on Nov 05, 2006 at 00:37 UTC
    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