这是我得到的字符串之一:str ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'我想要的是:['Name', 'coordinates','CITIZENS','colonisation']我正在尝试获取字符串中的单词,例如名称、坐标、公民、殖民化及其原始大小写。我尝试了拆分方法来删除下划线并使它们成为单个单词。,但效果不佳。我怎样才能做到这一点?
2 回答

慕娘9325324
TA贡献1783条经验 获得超4个赞
哟可以为此使用正则表达式:
import re
text ='_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
re.findall('_(\w*)_', text)
注意str是一个内置的 python 函数,不要用于变量名

婷婷同学_
TA贡献1844条经验 获得超8个赞
正则表达式应该可以解决问题:
import re
s = '_Name_ created _coordinates_ so that _CITIZENS_ would learn _colonisation_.'
result = re.findall('_(\w+)_', s)
添加回答
举报
0/150
提交
取消