0

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.

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

1 Answer 1

1

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"]]
Sign up to request clarification or add additional context in comments.

6 Comments

@marcog, thanks! There are other keywords in my text, which I would like to parse after splitting into tasks. General form /^([a-z])\:/i
@Andrei Where are the other keywords? Please give an example.
@marcog, Just did it. Please, see the updated question! I think I better split by task and then each of them by (sub)*task - then the problem resolves.
@Andrei And what do you want to get out? (Side note: Please ask what you really need help with from the beginning. Read this).
@Andrei The reason for that is (task): (in ...Ctask:D) matches as a separator. This returns the token task because it's in a group, then splits ...Ctask:D into ...C and D.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.