使用python 来做正则表达式:
import re
txt = "The rain in Spain aint"
#Check if "ain" is present, but NOT at the end 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"ain\B", txt)
print('r"ain\B" 结果: ',x)
if x:
print("Yes, there is at least one match not at the end of a word!")
else:
print("No match")
#Check if "ain" is present, but NOT at the beginning 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"\Bain", txt)
print('r"\Bain" 结果:',y)
if y:
print("Yes, there is at least one match not at the beginning of a word!")
else:
print("No match")
字符 \B 意思:Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word
结果如下:
如果觉得不错,就点赞或者关注或者留言~
谢谢~