1.使用正则完成下列内容的匹配
-
匹配陕西省区号 029-12345
-
匹配邮政编码 745100
-
匹配身份证号 62282519960504337X
python
import re
pattern_area_code = r'^029-\d+$'
pattern_postcode = r'^\d{6}$'
pattern_email = r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$'
pattern_id_card = r'^\d{17}[\dXx]$'
test_data = {
"区号": ["029-12345", "029-678901", "010-12345", "02912345", "029-abc"],
"邮政编码": ["745100", "100000", "74510", "7451001", "74510a"],
"邮箱": ["lijian@xianoupeng.com", "li_jian123@test.com.cn", "lijian@xianoupeng", "lijian@.com", "lijianxianoupeng.com"],
"身份证号": ["62282519960504337X", "62282519960504337x", "62282519960504337", "62282519960504337XA", "622825199605043378"]
}
def test_regex(pattern, test_list, name):
for content in test_list:
result = re.match(pattern, content)
if result:
print(f"匹配成功:{content}")
else:
print(f"匹配失败:{content}")
if __name__ == "__main__":
test_regex(pattern_area_code, test_data["区号"], "陕西省区号")
test_regex(pattern_postcode, test_data["邮政编码"], "邮政编码")
test_regex(pattern_email, test_data["邮箱"], "邮箱")
test_regex(pattern_id_card, test_data["身份证号"], "身份证号")

2.爬取学校官网,获取所有图片途径并将路径存储在本地文件中,使用装饰器完成