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"]
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With