3 回答
TA贡献1813条经验 获得超2个赞
您可以使用以下模式:
ing(?=\w)
这仅适用于包含whiching且后跟另一个单词字符的单词。这是一个例子:
inp = 'singer'
if re.search(r'ing(?=\w)', inp):
print('singer is a MATCH')
inp = 'sing'
if re.search(r'ing(?=\w)', inp):
print('sing is a MATCH')
这打印:
singer is a MATCH
编辑:
要匹配包含非终结符的整个单词ing,我建议使用re.findall:
inp = "Madonna is a singer who likes to sing."
matches = re.findall(r'\b\w*ing\w+\b', inp)
print(matches) # prints ['singer']
TA贡献1820条经验 获得超2个赞
如果单词不能以 ing 结尾但必须包含 ing:
\b\w*ing(?!\w*ing\b)\w+
解释
\b
一个字边界\w*
匹配0+单词字符ing
匹配所需的ing
(?!\w*ing\b)
否定前瞻,断言 theing
不在单词末尾\w+
匹配 1+ 个单词字符,因此后面必须至少有一个字符
例如
import re
items = ["singer","singing","ing","This is a ing testing singalong"]
pattern = r"\b\w*ing(?!\w*ing$)\w+\b"
for item in items:
result = re.findall(pattern, item)
if result:
print(result)
输出
['singer']
['singalong']
TA贡献1847条经验 获得超11个赞
您可以使用此模式:
import re
pattern = re.compile('\w*ing\w+')
print(pattern.match('sing')) # No match
print(pattern.match('singer')) # Match
添加回答
举报