使用python来做正则表达式:
import re
txt = "The rain in Spain ainb"
#Check if "ain" is present at the beginning of a WORD:
# the "r" in the beginning is making sure that the string is being treated as a "raw string"
x = re.findall(r"\bain", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
#Check if "ain" is present at the end of a WORD:
# the "r" in the beginning is making sure that the string is being treated as a "raw string"
y = re.findall(r"ain\b", txt)
print(y)
if y:
print("Yes, there is at least one match!")
else:
print("No match")
字符 \b: Returns a mach where the specified characters are at the beginning or at the end of a word.
结果如下:
如果觉得不错,就点赞或者关注或者留言~
谢谢~