从位于下划线之间的字符串中获取某些单词

这是我得到的字符串之一:

str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'

我想要的是:

['Name', 'coordinates','CITIZENS','colonisation']

我正在尝试获取字符串中的单词,例如名称、坐标、公民、殖民化及其原始大小写。

我尝试了拆分方法来删除下划线并使它们成为单个单词。,但效果不佳。

我怎样才能做到这一点?


哈士奇WWW
浏览 73回答 2
2回答

慕娘9325324

哟可以为此使用正则表达式:import retext ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'re.findall('_(\w*)_', text)注意str是一个内置的 python 函数,不要用于变量名

婷婷同学_

正则表达式应该可以解决问题:import res = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'result = re.findall('_(\w+)_', s)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python