Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Split string at character difference using regex

I'm current working on a problem that involves splitting a string by each group of characters.

For example,

"111223334456777" #=> ['111','22','333','44','5','6','777']

The way I am currently doing it now is using a enumerator and comparing each character with the next one, and splitting the array that way.

res = []
str = "111223334456777"
group = str[0]
(1...str.length).each do |i|
  if str[i] != str[i-1]
    res << group
    group = str[i]
  else
    group << str[i]
  end
end
res << group
res #=> ['111','22','333','44','5','6','777']

I want to see if I can use regex to do this, which will make this process a lot easier. I understand I could just put this block of code in a method, but I'm curious if regex can be used here.

So what I want to do is

str.split(/some regex/)

to produce the same result. I thought about positive lookahead, but I can't figure out how to have regex recognize that the character is different.

Does anyone have an idea if this is possible?

like image 378
davidhu Avatar asked Feb 28 '26 23:02

davidhu


1 Answers

The chunk_while method is what you're looking for here:

str.chars.chunk_while { |b,a| b == a }.map(&:join)

That will break anything where the current character a doesn't match the previous character b. If you want to restrict to just numbers you can do some pre-processing.

There's a lot of very handy methods in Enumerable that are worth exploring, and each new version of Ruby seems to add more of them.

like image 112
tadman Avatar answered Mar 03 '26 13:03

tadman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!