【Python网络爬虫】150讲轻松搞定Python网络爬虫付费课程笔记 篇十二——正则表达式匹配案例:手机号/邮箱/url/身份证

import re

# 验证手机号, 要求以1开头, 中间是23456789任意一位,
text = "18677889900"
result = re.match('1[23456789]\d{9}', text)
print(result.group())

# # 验证邮箱
text = "[email protected]"
result = re.match('\w+@[a-z0-9]+\.[a-z]+', text)
print(result.group())

# 验证url, 规则: http/https/ftp/ : // 任意非空字符
text = "https://www.baidu.com/index.html"
result = re.match('(http|https|ftp)://\S+', text)
print(result.group())

# 验证身份证, 规则: 18位, 前面17位时意数字,后面一个是数字/x/X
text = "345623200006202345x"
result = re.match('\d{17}[\dxX]', text)
print(result.group())

猜你喜欢

转载自blog.csdn.net/weixin_44566432/article/details/108691580