Using regex in Ruby 1.8.7 +:
p s.scan(/((\d)\2*)/).map(&:first) #=> ["111", "22", "1"]
This works because (\d) captures any digit, and then \2* captures zero or more, which corresponds to that group (second opening bracket). External (β¦) necessary to capture the entire match as a result in scan . Finally, only scan returned:
[["111", "1"], ["22", "2"], ["1", "1"]]
... so we need to run through and save only the first element in each array. In Ruby 1.8.6+ (there is no Symbol#to_proc for convenience):
p s.scan(/((\d)\2*)/).map{ |x| x.first } #=> ["111", "22", "1"]
Without Regex, here's an interesting (matching any char) that works in Ruby 1.9.2:
p s.chars.chunk{|c|c}.map{ |n,a| a.join } #=> ["111", "22", "1"]
Here's another version that should work even in Ruby 1.8.6:
p s.scan(/./).inject([]){|a,c| (a.last && a.last[0]==c[0] ? a.last : a)<<c; a } # => ["111", "22", "1"]
source share