Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Elegant way to split into sequences of identical chars?

by japhy (Canon)
on Nov 29, 2005 at 21:23 UTC ( #512771=note: print w/replies, xml ) Need Help??

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; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://512771]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-01-24 04:23 GMT
Find Nodes?
    Voting Booth?