From: MonkeeSage Date: 2007-12-16T19:50:30+09:00 Subject: Re: Splitting string into array keeping delimiters On Dec 15, 9:16 am, Gary C40 wrote: > Hi, I've been playing Ruby for a few months now. > Yesterday I came across an interesting problem. > If I have this string: > abcd1234abc123 > Now I want to separate the digit group with the non-digit group into an > array like this ["abcd",1234,"abc",123]. It's like re.split in Python. > How can I do it in Ruby with the least lines of code possible? > 'abcd1234abc123'.split(/\d+/) only returns ["abcd","abc"] > Thank you in advance > -- > Posted viahttp://www.ruby-forum.com/. And the highly esoteric version... n = [[]] s = [[]] 'abcd1234abc123'.each_byte { | x | if (47..57).include?(x) then s << []; n.last << x else n << []; s.last << x end } n = n.reject { | x | x.empty? }.map { | x | x.map {| y | y.chr }.join("").to_i } s = s.reject { | x | x.empty? }.map { | x | x.map { | y | y.chr }.join("") } result = if n.length > s.length then n.zip(s).flatten.compact else s.zip(n).flatten.compact end p result # => ["abcd", 1234, "abc", 123] Regards, Jordan