Python程序设计基础(第3版)——习题及答案 【ch10】正则表达式与简单爬虫.docx

Python程序设计基础(第3版)——习题及答案 【ch10】正则表达式与简单爬虫.docx

第十章正则表达式与简单爬虫 1.根据下列字符串的构成规律写出正则表达式,并尝试利用re库中的有关方法实现对测试字符串的匹配、搜索、切分、分组和替换操作。 (1)E-mail地址。 答案: import re email_pattern = r^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ test_string = example@ if re.match(email_pattern, test_string): print(匹配成功:, test_string) else: print(匹配失败:, test_string) (2)IPv4地址。 答案: import re ipv4_pattern = r^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ test_string = if re.match(ipv4_pattern, test_string): print(匹配成功:, test_string) else: print(匹配失败:, test_string) (3)国内手机号码。 答案: import re phone_pattern = r^1[3-9]\d{9}$ test_string =if re.match(phone_patter

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档