如何在 Haskell 中以 shell 风格的语法拆分字符串? Python 中的等价物是
shlex.split
.
>>> shlex.split('''/nosuchconf "/this doesn't exist either" "yep"''')
['/nosuchconf', "/this doesn't exist either", 'yep']
我不确定你到底是什么意思:你想从字符串中获取所有引用的子字符串吗?请注意,与 Python 等不同,Haskell 只有一组引号表示某物是字符串,即
"..."
.
要考虑的可能性:
如果您指定了为什么需要这样的功能,这可能会有用:您是否正在尝试解析现有的 shell 脚本?那么 language-sh 可能会有用。但是你不应该在 Haskell 内部使用这样的字符串,而是使用
[String]
或其他东西。
https://hackage.haskell.org/package/shellwords
λ> import ShellWords
λ> parse "/nosuchconf \"/this doesn't exist either\" \"yep\""
Right ["/nosuchconf","/this doesn't exist either","yep"]