import re
text = "Hello,I am linheng.I am a student."
matchObj1 = re.findall(r'\bhe\b',text)#表示he这个单词,结果是无匹配
if matchObj1:
print(matchObj1)
else:
print('not match')
matchObj2 = re.findall(r'\bhe',text) ##['he'],出自heng,\bhe表示以he开头的
if matchObj2:
print(matchObj2)
else:
print('not match')
matchObj3 = re.findall(r'\bHello\b',text)#匹配整个单词Hello
if matchObj3:
print(matchObj3)
else:
print('not match')
matchObj4 = re.findall(r'[Hh]e',text)#匹配以He或者he开头的
if matchObj4:
print(matchObj4)
else:
print('not match')
结果如下:
not match
not match
['Hello']
['He', 'he']