fparsec解析字符串序列 [英] fparsec to parse sequence of string

查看:58
本文介绍了fparsec解析字符串序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入的文本,例如"abc,def,ghi".我想解析它以获得["abc","def"]的字符串列表.

I have an user input text like "abc,def,ghi". I want to parse it to get list of string as ["abc", "def"].

我尝试了

let str : Parser<_> = many1Chars (noneOf ",")
let listParser : Parser<_> = many (str);;

但是它总是只给我第一项["abc"]. "Def"和其他不在列表中

but it always give me the first item only ["abc"]. "Def" and others are not coming in result list

推荐答案

您要解析第一个逗号,而不是解析逗号本身.

You're parsing up to the first comma, but not parsing the comma itself.

要解析由其他内容分隔的内容列表,请使用 sepBy :

To parse a list of things separated by other things, use sepBy:

let comma = pstring ","
let listParser = sepBy str comma

如果您需要解析至少一个",请改用sepBy1.

If you need to parse "at least one", use sepBy1 instead.

这篇关于fparsec解析字符串序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆