Reputation: 577
Based on my previous question, I've come up with a better way to approach my problem. Here's what I have in mind:
Here's some sample input:
This will be a new entry | param1 |param2 |etc.
This is another entry, but without params
This is a third entry|with a twist
| I'm using subsequent lines for
| its parameters.
Yet I still want the next line to be another new entry.
And this should be the output:
Entry #1: "This will be a new entry"
Params: ["param1","param2","etc."]
Entry #2: "This is another entry, but without params"
Entry #3: "This is a third entry"
Params: ["with a twist","I'm using subsequent lines for","its parameters."]
Entry #4: "Yet I still want the next line to be another new entry."
What would be a good way to go about doing this?
Upvotes: 1
Views: 80
Reputation: 59993
At this point, you should consider writing a proper grammar and using a parser generator instead of hacking up regexes to do the job.
Even if you are going for regexes, trying to come up with some miraculous one-liner that does the whole job is going to result in something hideous.
Instead, consider something like the following psuedocode:
foreach (line of input)
If the first non-whitespace character is NOT a delimiter
output what we have so far, then parse out the title of the next entry
while there's still text on this line
grab up to the next delimiter, parse as a parameter.
Upvotes: 1