I'm working on building a parser with the SuperPower library.
Here's a sample of the source input that I want to parse:
|ABC|xyz|1 3|~~~|
- The First
|is theOpeningPipe. - The last
|is theClosingPipe. - The 2nd, 3rd, and 4th
|are delimiters. - Ultimately, I would want to be able to produce this structure during parsing:
new string[]{"ABC", "xyz", "1 3", "~~~"}
I think that the trouble I'm having is that my delimiter character is the same as my ClosingPipe character.
How should I build this TextParser<string[]>?
Here are some parsers that should work for you:
Then use it like so:
And
resultshould be{ "ABC", "xyz", "1 3", "~~~" }The idea is to parse out the opening pipe, first content, then the closing pipe, and then since (I'm assuming) the rest of the number of delimiters can change, you can use Superpower's built in method
ManyDelimitedByto parse out as many other content pieces as there are, separated by pipes. But since your input always has a pipe at the end, theManyDelimitedBywill leave an empty string at the end of thecontentsarray, which I'm removing before returning the final output.EDIT
Here is a way to do it without having to chop off the empty string: