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"]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
splitwill just use whatever it matches as a delimiter, removing it from the string in question. What you’re looking for isscan:Taking it slow, the
\dmatches 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 likeSince 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.)