http://www.perlmonks.org?node_id=512771


in reply to Elegant way to split into sequences of identical chars?

This is a little crufty, I'll admit, but I like it!
sub splitter { local $_ = shift; split /(?<!^)(?!(??{ quotemeta substr($_, $-[0]-1, 1) }))/; }
This splits a string at all locations that 1) are not the beginning of the string, and 2) are not followed by the character immediately preceding this location.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Elegant way to split into sequences of identical chars?
by tphyahoo (Vicar) on Dec 05, 2005 at 09:50 UTC
    A little less obfuscated if you use extended regex comments. (I liked this solution a lot, because I feel like it gets to the "core" of what is wanted.)

    use strict; use warnings; use Data::Dumper; print join " ", splitter('xx((556xx'); sub splitter { local $_ = shift; split / (?<!^) #not preceeded by + start of string. (?! #not followed by... (??{ quotemeta substr($_, $-[0]-1, 1) # the escaped +(quote-metad) last character of the last match. # note: $-[0] is th +e offset of start of last successful match. # $-[1] (not used h +ere) would be the offset of start of the first sub pattern. }) ) /x; }