I am learning parsec, and just encountered the following situation. I want to separate a String
into [String]
by a specific String
; for example, I get "abcSEPdef
, and the separator is "SEP", so, after being parsed, I should get a ["abc","def"]
I believe the parser should look like sepBy a_parser (string "SEP")
; however, I don't know how the a_parser
should be like.
Using manyTill
a few times will work:
uptoSEP = manyTill anyChar (eof <|> (string "SEP" >> return ()))
splitSEP = manyTill uptoSEP eof
E.g.:
ghci> parseTest splitSEP "abcSEPdefSEPxyz"
["abc","def","xyz"]
You'll want to enable the {-# LANGUAGE NoMonomorphismRestriction #-}
pragma.