python - 在空格或连字符上拆分?

在 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/

相关文章:

swift - 在 Swift 中使用 %@ 格式化字符串

c# - String.Format 参数顺序烦人

haskell - 如何将 Rational 转换为 "pretty"字符串?

c# - Html.HiddenFor 在 ASP.NET 中错误地格式化 DateTime

php - 如何使用 PHP 发送 HTML 格式的邮件?

reporting-services - SSRS 格式显示为百分比

c# - 在 DataGridView 列中格式化 TimeSpan

c++ - 格式化 C++ 控制台输出

perl - 使用 Perl 进行格式化输出的最佳方式

javascript - getHours() 返回不正确的值