使用python来表示正则表达式:
import re
txt = "2343 33 089 2 sdf"
#Check if the string has any three-digit numbers, from 000 to 199:
x = re.findall("[0-1][0-9][0-9]", txt)
print('numbers from 000 to 199 结果: ',x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
txt_new = "234 33 89 2 sdf"
#Check if the string has any two-digit numbers, from 00 to 59:
y = re.findall('[0-5][0-9]', txt_new)
print('numbers from 00 to 59 结果:',y)
结果如下:
解释一下结果:由于代码中用了[ ][ ]来做范围。比如[0-5]对应的是十位数所取的数字的范围为0-5,[0-9] 表示个位数的数字范围为0-9.
如果觉得不错,就点赞或者关注或者留言~
谢谢~