Search code examples
f#fparsec

fparsec to parse sequence of string


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


Solution

  • 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.