ruby通过重复字符或空格来分割字符串

时间:2022-06-01 19:53:40

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"] 

1 个解决方案

#1


25  

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

split将使用它匹配的任何东西作为分隔符,将其从所讨论的字符串中删除。你要找的是扫描:

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

慢慢来,\d匹配任何数字。它在第二个捕获组中,因此\2*将匹配任何进一步出现的相同数字。这会生成一个看起来像这样的数组

[["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).

由于我们只希望每个子数组中的第一个项,所以我们可以使用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.)

(注意,str.scan(/(\d)\1*/)只会从第一个捕获组中生成一个数组,这意味着我们只能从一个可能重复的数字序列中获得一个数字。)

#1


25  

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

split将使用它匹配的任何东西作为分隔符,将其从所讨论的字符串中删除。你要找的是扫描:

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

慢慢来,\d匹配任何数字。它在第二个捕获组中,因此\2*将匹配任何进一步出现的相同数字。这会生成一个看起来像这样的数组

[["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).

由于我们只希望每个子数组中的第一个项,所以我们可以使用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.)

(注意,str.scan(/(\d)\1*/)只会从第一个捕获组中生成一个数组,这意味着我们只能从一个可能重复的数字序列中获得一个数字。)