在 Python 中,如何拆分空格或连字符?
输入:
You think we did this un-thinkingly?
期望的输出:
["You", "think", "we", "did", "this", "un", "thinkingly"]
我能走多远
mystr.split(' ')
但我不知道如何分割连字符和空格 and the Python definition of split only seems to specify a string .我需要使用正则表达式吗?
最佳答案
如果您的模式对于一个(或可能两个)replace
来说足够简单,请使用它:
mystr.replace('-', ' ').split(' ')
否则,请按照 @jamylak 的建议使用 RE .
https://stackoverflow.com/questions/16926870/