Python 正则表达式
正则表达是在Python中使用很广泛, 例如, 爬取了一些数据,你需要对这些数据进行分类, 正则表达式就扮演了一个较为重要的角色。
正则表达式匹配原理
1 一个正则表达式 编译成 正则表达式对象
2. 对象和一个文本进行匹配, 最后得到匹配结果。
import re
# encoding: UTF-8
'''
正则表达式匹配文本顺序:
1.正则表达式 编译成正则表达式对象
2.正则表达式对象 和文本进行匹配 ,得到匹配结果
'''
regex = re.compile("(\d)*",re.I)
s ="123456"
s1 = "hello world!"
match = regex.match(s).group()
print('s is :{0}'.format(match) )
match = regex.match(s1)
print('s1 is :{0}'.format(match) )
先盗图一张:
正则表达式 使用范例:
校验手机号:
s = '15645237856'
reg = re.compile('^[1][3-8]*[0-9]{9}$',re.I)
match = reg.match(s)
if match != None:
print("s is :",match.group())
else:
print(match)