在 Haskell 中拆分类似 shell 的语法?

问题描述 投票:0回答:2

如何在 Haskell 中以 shell 风格的语法拆分字符串? Python 中的等价物是

shlex.split
.

>>> shlex.split('''/nosuchconf "/this doesn't exist either" "yep"''')
['/nosuchconf', "/this doesn't exist either", 'yep']
python haskell split lexical-analysis shlex
2个回答
1
投票

我不确定你到底是什么意思:你想从字符串中获取所有引用的子字符串吗?请注意,与 Python 等不同,Haskell 只有一组引号表示某物是字符串,即

"..."
.

要考虑的可能性:

  • wordslines功能

  • split

  • 使用 polyparse、uu-parsinglib、parsec 等编写自定义解析器

如果您指定了为什么需要这样的功能,这可能会有用:您是否正在尝试解析现有的 shell 脚本?那么 language-sh 可能会有用。但是你不应该在 Haskell 内部使用这样的字符串,而是使用

[String]
或其他东西。


0
投票

https://hackage.haskell.org/package/shellwords

λ> import ShellWords
λ> parse "/nosuchconf \"/this doesn't exist either\" \"yep\""
Right ["/nosuchconf","/this doesn't exist either","yep"]
© www.soinside.com 2019 - 2024. All rights reserved.