流式结构化文本输入
我想从 TextReader
解析格式化的基本值和一些自定义字符串 - 本质上就像 scanf
允许的那样。
- 我的输入可能没有换行符,因此 ReadLine+Regex 不是一个选项。我可以使用其他方式对文本输入进行分块;但问题是我在编译时不知道分隔符(所以这很棘手),并且该分隔符可能与本地化相关。例如,后跟逗号的浮点数可能是“1.5”或“1,5”,但在这两种情况下尝试解析浮点数都应该是“贪婪的”。
- 为了安全起见,我想假设我的输入是积极敌对的(例如,从网络流流入):即故意缺少分块分隔符。
- 我想避免自定义正则表达式:int.Parse 和 double.Parse 工作良好并且具有本地化意识。不要让我开始使用 DateTime - 无论如何,我可能需要一些自定义模式,但编写正则表达式来覆盖该场景听起来并不有趣。
举一个具体的例子,假设我有一个 TextReader 并且我知道下一个值应该是 double - 我如何提取该 double 以及可能有限数量的前瞻无需读取整个流,也无需手动编写可本地化的双解析器?
类似的问题
有一个上一个问题“寻找与 scanf
等效的 C# ”听起来很相似,但问答集中在 readline+regex (我想避免)。 如何对 TextReader 使用正则表达式? 没有找到答案(除了分块之外),无论如何我都想避免编写自己的正则表达式。
I'd like to parse formatted basic values and a few custom strings from a TextReader
- essentially like scanf
allows.
- My input might not have line-breaks, so ReadLine+Regex isn't an option. I could use some other way of chunking text input; but the problem is that I don't know the delimiter at compile time (so that's tricky), and that that delimiter might be localization-dependant. For instance, a float followed by a comma might be "1.5," or "1,5," but in both cases attempting to parse the float should be "greedy".
- To be safe, I'd like to assume my input is actively hostile (say, streaming in from a network stream): i.e. intentionally missing chunking delimiters.
- I'd like to avoid custom Regex's: int.Parse and double.Parse work well and are localization-aware. Don't get me started on DateTime's - I might need a few custom patterns anyhow, but writing Regexes to cover that scenario doesn't sound like fun.
For a concrete example, let's say I have a TextReader
and that I know the next value should be a double
- how can I extract that double and possibly a limited amount of lookahead without reading the entire stream and without manually writing a localizable double-parser?
Similar Questions
There's a previous question "Looking for C# equivalent of scanf
" which sounds similar but the Q+A focus on readline+regex (which I'd like to avoid). How can I use Regex against a TextReader? didn't find an answer (beyond chunking), and in any case I'd like to avoid writing my own Regexes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于缺乏答案并且我自己仍然没有找到任何东西,似乎
Based on that lack of answers and still not having found anything myself, it seems that