开发者

Splitting a string by a repeated regex in Ruby

I would like to parse text and separate it into tasks and subtasks:

'Asubsubsubtask:Bsubtask:Ctask:D'.split(/((sub)*task)\:/i)
#=> ["A", "subsubsubtask", "sub", "B", "subtask", "sub", "C", "task", "D"]

The last part of the result array is not consistent and doesn't allow me to use #each_slice(3) processing the arra开发者_如何学Goy.

What would you suggest me to use instead of matching each element of the array with a similar regex?

EDIT1:

More detailed example:

Task: Main
description
Defaults: some params

Subtask: Basic
description
Options: A B C

Subsubtask: Reading
description
Parameters: some params

and I try to split it by /^((sub)*task)\:/i


Separate it into two split calls:

irb(main):007:0> 'Asubsubsubtask:Bsubtask:Ctask:D'.split(':').collect{|s| s.split(/((sub)*task)/i)}
=> [["A", "subsubsubtask", "sub"], ["B", "subtask", "sub"], ["C", "task"], ["D"]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜