Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby split string by repeating characters or a space

Tags:

regex

ruby

How do i split this string.

"6885558 8866887777" => ["6", "88", "555", "8", "88", "66", "88", "7777"] 

I tried this, but it never worked.

ruby-1.8.7-p334 :020 > "111133".split(/(\d)\1+/)
 => ["", "1", "", "3"] 
like image 741
Deepak Avatar asked Feb 15 '12 11:02

Deepak


People also ask

How do you split a character in a string in Ruby?

split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches.

How do you split an array of strings in Ruby?

The general syntax for using the split method is string. split() . The place at which to split the string is specified as an argument to the method. The split substrings will be returned together in an array.


1 Answers

split will just use whatever it matches as a delimiter, removing it from the string in question. What you're looking for is scan:

str = "6885558 8866887777"
str.scan(/((\d)\2*)/).map(&:first)
# => ["6", "88", "555", "8", "88", "66", "88", "7777"]

Taking it slow, the \d matches any digit. It's in the second capturing group, so \2* then matches any further occurrences of the same digit. This produces an array that looks like

[["6", "6"], ["88", "8"], ["555", "5"], ["8", "8"],
 ["88", "8"], ["66", "6"], ["88", "8"], ["7777", "7"]]

Since we only want the first item in each of those sub arrays, we can collect them all with map(&:first).

(Note that str.scan(/(\d)\1*/) would simply produce an array out of the first capturing group, which means we'd only get one digit from a sequence of possibly repeated numbers.)

like image 169
brymck Avatar answered Nov 14 '22 04:11

brymck