Java 的 shlex 替代品
Java 是否有 shlex 替代方案? 我希望能够像 shell 处理它们一样分割引号分隔的字符串。 例如,如果我发送:
one two "three four"and perform a split, I'd like to receive the tokens
one
two
three four
Is there a shlex alternative for Java? I'd like to be able to split quote delimited strings like the shell would process them. For example, if I'd send :
one two "three four"
and perform a split, I'd like to receive the tokens
one
two
three four
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我今天遇到了类似的问题,看起来任何标准选项(例如 StringTokenizer、StrTokenizer、Scanner)都不适合。 然而,实施基础知识并不难。
此示例处理当前在其他答案中评论的所有边缘情况。 请注意,我还没有检查它是否完全符合 POSIX 要求。 Gist 包括 GitHub 上提供的单元测试 - 通过未经许可在公共领域发布。
I had a similar problem today, and it didn't look like any standard options such as StringTokenizer, StrTokenizer, Scanner were a good fit. However, it's not hard to implement the basics.
This example handles all the edge cases currently commented on other answers. Be warned, I haven't checked it for full POSIX compliance yet. Gist including unit tests available on GitHub - released in public domain via the unlicense.
查看 Apache Commons Lang:
org.apache.commons.lang.text.StrTokenizer 应该能够做你想做的事:
Look at Apache Commons Lang:
org.apache.commons.lang.text.StrTokenizer should be able to do what you want:
我使用 fastparse 成功使用了以下 Scala 代码。 我不能保证它是完整的:
I had success using the following Scala code using fastparse. I can't vouch for it being complete: