如何使用 StreamTokenizer 确保特定标记之间有空格?
我正在使用 StreamTokenizer 来解析文本,我需要确保 某些标记之间有空格。例如,“5+5”是非法的,但“5 + 5”是有效的。
我真的不太了解 StreamTokenizer
;我阅读了 API,但找不到任何可以帮助我的东西。我该怎么做?
I am working with StreamTokenizer
to parse text, and I need to make sure that
there's whitespace between certain tokens. For example, "5+5" is illegal, but "5 + 5" is valid.
I really don't know StreamTokenizer
that well; I read the API but couldn't find anything to help me. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将空格设置为“普通字符”。这意味着它们将作为代币单独返回,但不会折叠到其他代币中。示例:
不幸的是,您必须在解析器中处理空白标记。我建议滚动你自己的标记器。除非您正在做一些快速而肮脏的事情,否则 StreamTokenizer 几乎总是错误的选择。
You'll have to set spaces as "ordinary characters". This means that they'll be returned as tokens on their own, but not folded into other tokens. Example:
Unfortunately, you'll have to deal with whitespace tokens in your parser. I'd recommend rolling your own tokenizer. Unless you're doing something quick and dirty, StreamTokenizer is almost always the wrong choice.
此后考虑使用扫描仪。例如:
这是一个简化的示例,假设输入如
"5+5"
或"5 + 5"
。您应该随时检查scanner.hasNext()
,也许在where
循环中检查以处理更复杂的输入。Consider using
Scanner
instead since. E.g.:This is a simplified example that assumes an input like
"5+5"
or"5 + 5"
. You should be checkingscanner.hasNext()
as you go, perhaps in awhere
loop to process more complicated inputs.