2017-08-04 70 views
0

我有一个任意字符串的可变长度数组。一个一致性是字符串“你好”重复,我想分组的字符串“你好”。按重复值拆分数组

所以给出这样的:

[ 
"hello\r\n", 
"I\r\n", 
"am\r\n", 
"Bob\r\n", 
"hello\r\n", 
"How\r\n", 
"are you?\r\n" 
] 

我想这一点:

[ 
[ 
    "hello\r\n", 
    "I\r\n", 
    "am\r\n", 
    "Bob\r\n" 
], 
[ 
    "hello\r\n", 
    "How\r\n", 
    "are you?\r\n" 
] 
] 

我曾尝试:

partition = [] 
last = input.size 
index = 0 
input.each_with_object([]) do |line, acc| 
    index += 1 
    if line == "hello\r\n" 
    acc << partition 
    partition = [] 
    partition << line 
    else 
    partition << line 
    end 
    if index == last 
    acc << partition 
    end 
    acc 
end.delete_if(&:blank?) 
=> [["hello\r\n", "I\r\n", "am\r\n", "Bob\r\n"], ["hello\r\n", "How\r\n", "are you?\r\n"]] 

结果是正确的,但有可能做什么我想用ruby数组迭代器?我的解决方案似乎笨重。

+0

什么是想要的返回值,如果在字符串'[ “咳咳\ r \ n”, “你好\ r \ n”, “我用\ r \ n”, “AM \ r \ n” ,“Bob \ r \ n”,“chellos \ r \ n”,“你好吗?\ r \ n”]?当你举一个例子时,给每个输入分配一个变量是很有帮助的(例如,'arr = [“hello \ r \ n”,...]')这使得读者可以在回答和评论中引用变量而无需定义它们。 –

回答

5

您可以使用Enumerable#slice_before

arr.slice_before { |i| i[/hello/] }.to_a  
#=> [["hello\r\n", "I\r\n", "am\r\n", "Bob\r\n"], 
# ["hello\r\n", "How\r\n", "are you?\r\n"]] 

或更简洁(由@tokland的建议):

arr.slice_before(/hello/).to_a 
1

这里是不使用Enumerable#slice_before,这是在Ruby中v介绍的方法。 2.2。它适用于v1.9 +(如果each_with_object被替换为reduce/inject,则可以与v1.87 +一起使用)。

假设

我假设:

  • 所有字符串用“你好”被丢弃
  • 匹配开头的第一个字符串前面的“你好”字符串必须开始“你好”和不能仅仅包含你好的词(例如,“hellonfire”)

C颂

def group_em(arr, target) 
    arr.each_with_object([]) { |s,a| (s =~ /\A#{target}(?!\p{alpha})/) ? 
    (a << [s]) : (a.last << s unless a.empty?) } 
end 

arr = ["Ahem\r\n", "hello\r\n", "I\r\n", "hello again\r\n", "am\r\n", 
     "Bob\r\n", "hellonfire\r\n", "How\r\n", "are you?\r\n"] 

group_em(arr, 'hello') 
    #=> [["hello\r\n", "I\r\n"], 
    # ["hello again\r\n", "am\r\n", "Bob\r\n", "hellonfire\r\n", 
    #  "How\r\n", "are you?\r\n"]] 

注意"Ahem\r\n"不包括在内,因为它不遵循"hello""hellonfire\r\n"不会触发新的切片,因为它不匹配'“你好”`` 。

讨论

在该实例中,正则表达式被计算为等于

/(?m-ix:\Ahello(?!\p{alpha}))/ 

它可以代替在自由间隔模式被定义,使其自文档。

/ 
\A    # match the beginning of the string 
#{target}  # match target word 
(?!\p{alpha}) # do not match a letter (negative lookbehind) 
/x    # free-spacing regex definition mode