re模块相关方法:
下面给大家演示re模块中常见的方法使用
re.match:
n match:从头开始匹配
n 需求:字符串前5位必须是数字 实现过程:
示例:
使用re.match方法正则表达式;r'\d{5}':5个数字, \d代表数字(0~9),{5}代表出现5次;
返回match对象。
s1 = '12345abcd'
s2 = '123abcdef'
match2 = re.match(r'\d{5}', s1)
match3 = re.match(r'\d{5}', s2)
print(match2)
print(match3)结果, match3为None:
None