【发布时间】:2021-10-29 07:23:58
【问题描述】:
我有不同长度的字符串,必须检查匹配“tion”、“ex”、“ph”、“ost”、“ast”、“ist”模式的子字符串,忽略大小写和位置,即前缀/后缀/单词中间。必须在新列表中返回匹配的单词,而不是仅在匹配的子字符串元素中返回。使用下面的代码,我可以返回一个没有完整匹配词的匹配子字符串元素的新列表。
def latin_ish_words(text):
import re
pattern=re.compile(r"tion|ex|ph|ost|ast|ist")
matches=pattern.findall(text)
return matches
latin_ish_words("This functions as expected")
结果如下:['tion', 'ex']
我想知道如何将整个单词而不是匹配的子字符串元素返回到新列表中?
【问题讨论】:
标签: python regex string substring findall