Example:
I have the literals “alpha”, “beta”, “gamma”. How do I make pyparsing parse the following inputs:
alpha
alpha|beta
beta|alpha|gamma
The given input can be constructed by using one or more non-repeating literals from a given set, separated by “|”. Advice on setting up pyparsing will be appreciated.
Use the ‘&’ operator for Each, instead of ‘+ or ‘|’. If you must have all, but in unpredicatable order use:
If some may be missing, but each used at most once, then use Optionals:
Oops, I forgot the ‘|’ delimiters. One lenient parser would be to use a delimitedList:
This would allow any or all of your choices, but does not guard against duplicates. May be simplest to use a parse action:
This feels like the simplest approach to me.
EDIT:
Recent versions of pyparsing have introduced parse-time conditions to make this kind of parse action easier to write: