解析f#中的分隔仪分离值
我正在以此形式解析字符串记录:
description: double/double/double
对于Ex。:
item description: 0.4/8/-24.66
每个记录都应导致一个对象:
new MyObj("first item", 0.4, 2.4, -24.66)
我可以通过正则表达式和程序方法轻松地执行此操作,但是结果代码容易出错,却相当丑陋(至少来自功能的观点)。
如何以更优雅的方式完成这件事?
I am parsing string records in this form:
description: double/double/double
for ex.:
item description: 0.4/8/-24.66
Each record should result in an object:
new MyObj("first item", 0.4, 2.4, -24.66)
I could easily do this with a regular expression and a procedural approach, but the resulting code is error-prone and rather ugly (at least from a functional point of view).
How can this be accomplished in a more elegant way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,FParsec 很好:
然后
run parser "0.4/8/-24.66"
返回val it : ParserResult; = 成功:[0.4; 8.0; -24.66]
。Well, FParsec is nice:
And then
run parser "0.4/8/-24.66"
returnsval it : ParserResult<float list,unit> = Success: [0.4; 8.0; -24.66]
.或者,您只需创建一个简单的解析器函数
string-&gt; myobj
像这样:(update:我猜想记录是由新线或类似的东西隔开的被分为第二个想法。
Or you can just create a simple parser function
string->MyObj
like this:(update: I guessed that records were separated by a new-line or something like that and were split into a list. On the second thought, there's nothing like that in your question...)