From: Logan Capaldo Date: 2007-08-12T20:27:31+09:00 Subject: Re: Split a string based on change of character ------=_Part_21590_9545866.1186918054170 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/12/07, Simon Kr=F6ger wrote: > > Pe=F1a schrieb: > > From: William James [mailto:w_a_x_man@yahoo.com] > > # s =3D "ZBBBCZZ" > > # =3D=3D>"ZBBBCZZ" > > # s.scan( /((.)\2*)/ ).transpose.first > > # =3D=3D>["Z", "BBB", "C", "ZZ"] > > # s.gsub( /(.)(?!\1)/, "\\1\n" ).split > > # =3D=3D>["Z", "BBB", "C", "ZZ"] > > > > ruby hacker, James, that is cool! gotta keep this. > > kind regards -botp > > Yeah, nice! > > i think one can simplify from > > s.gsub( /(.)(?!\1)/, "\\1\n" ).split > > to > > s.gsub(/(.)\1*/, '\0 ').split > Yes it appears so. Another variation would be (this lets you use the method on strings that contain whitespace already correctly): require 'enumerator' s.enum_for(:gsub, /(.)\1*/).to_a Which is sort of back to the original scan method. ? > > cheers > > Simon > > > ------=_Part_21590_9545866.1186918054170--