Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to catch groups of same digits in Ruby

Is it possible to catch all grous of same digits in string with regex on Ruby? I'm not familiar with regex.

I mean: regex on "1112234444" will produce ["111", "22", "3", "4444"]

I know, I can use (\d)(\1*), but it only gives me 2 groups in each match. ["1", "11"], ["2", "2"], ["3", -], ["4", "444"]

How can I get 1 group in each match? Thanks.

like image 791
Airat Shigapov Avatar asked Oct 19 '12 11:10

Airat Shigapov


3 Answers

Here, give this a shot:

((\d)\2*)
like image 64
slackwing Avatar answered Oct 18 '22 02:10

slackwing


You can use this regex

((\d)\2*)

group 1 catches your required value

like image 33
Anirudha Avatar answered Oct 18 '22 01:10

Anirudha


My first quick answer was rightfully criticized for having no explanation for the code. So here's another one, better in all respects ;-)

We exploit the fact that the elements whose runs we want are digits and they are easy to enumerate by hand. So we construct a readable regex which means "a run of zeros, or a run of ones, ... or a run of nines". And we use the right method for the job, String#scan:

irb> "1112234444".scan(/0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/)
=> ["111", "22", "3", "4444"]

For the record, here's my original answer:

irb> s = "1112234444"
=> "1112234444"
irb> rx = /(0+|1+|2+|3+|4+|5+|6+|7+|8+|9+)/
=> /(0+|1+|2+|3+|4+|5+|6+|7+|8+|9+)/
irb> s.split(rx).reject(&:empty?)
=> ["111", "22", "3", "4444"]
like image 1
Martin Vidner Avatar answered Oct 18 '22 02:10

Martin Vidner