在空格或连字符上分开? [英] Split on either a space or a hyphen?

查看:91
本文介绍了在空格或连字符上分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何在空格或连字符之间分割?

In Python, how do I split on either a space or a hyphen?

输入:

You think we did this un-thinkingly?

所需的输出:

["You", "think", "we", "did", "this", "un", "thinkingly"]

我可以做到

mystr.split(' ')

但是我不知道如何分割连字符和空格 Python的split定义似乎只指定了一个字符串.我需要使用正则表达式吗?

But I don't know how to split on hyphens as well as spaces and the Python definition of split only seems to specify a string. Do I need to use a regex?

推荐答案

如果您的模式对于一个(或两个)replace足够简单,请使用它:

If your pattern is simple enough for one (or maybe two) replace, use it:

mystr.replace('-', ' ').split(' ')

否则,请按照 @jamylak 的建议使用RE.

Otherwise, use RE as suggested by @jamylak.

这篇关于在空格或连字符上分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆