fparsec to parse sequence of string

随声附和 提交于 2019-12-10 21:45:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!