2017-08-04 68 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