简体   繁体   English

在Ruby中通过重复的正则表达式拆分字符串

[英]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 array. 结果数组的最后一部分不一致,并且不允许我使用#each_slice(3)处理数组。

What would you suggest me to use instead of matching each element of the array with a similar regex? 您会建议我使用什么,而不用相似的正则表达式匹配数组的每个元素?

EDIT1: 编辑1:

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 我尝试用/^((sub)*task)\\:/i拆分它

Separate it into two split calls: 将其分为两个split调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM