Thursday, 5 September 2013

scanning for repeating digits with regex in Ruby

scanning for repeating digits with regex in Ruby

a = [1914, 1982, 2000, 1234, 0393]
How would I write a regex expression that would only extract numbers where
the digits are repeating? So in the case for array a it would be [1914,
2000, 0393]
b = [777302, 091111, 23900]
Also, how would I write a regex expression that would extract digits that
repeat only 3 times. So for array b it would be only 777302 since the
777's are repeating 3 times. I know writing it as
a.map(&:to_s).scan(/0{3}|.......|9{3}|/).map(&:to_i) probably works but
that's just ridiculous.

No comments:

Post a Comment