问题
I have an user input text like "abc,def,ghi". I want to parse it to get list of string as ["abc", "def"].
I tried
let str : Parser<_> = many1Chars (noneOf ",")
let listParser : Parser<_> = many (str);;
but it always give me the first item only ["abc"]. "Def" and others are not coming in result list
回答1:
You're parsing up to the first comma, but not parsing the comma itself.
To parse a list of things separated by other things, use sepBy:
let comma = pstring ","
let listParser = sepBy str comma
If you need to parse "at least one", use sepBy1
instead.
来源:https://stackoverflow.com/questions/41625973/fparsec-to-parse-sequence-of-string