From: William James Date: 2007-08-13T16:45:00+09:00 Subject: Re: Split a string based on change of character On Aug 12, 3:06 am, Simon Kr�ger wrote: > Pe�a schrieb: > > > From: William James [mailto:w_a_x_...@yahoo.com] > > # s = "ZBBBCZZ" > > # ==>"ZBBBCZZ" > > # s.scan( /((.)\2*)/ ).transpose.first > > # ==>["Z", "BBB", "C", "ZZ"] > > # s.gsub( /(.)(?!\1)/, "\\1\n" ).split > > # ==>["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 > > ? > > cheers > > Simon Yes, with the possible exception of "\\1\n" . I was anticipating the need to allow the string to contain any character but a newline. s = 'ZBBBC ZZ' ==>"ZBBBC ZZ" s.gsub(/(.)\1*/, "\\0\n").split("\n") ==>["Z", "BBB", "C", " ", "ZZ"]