练习4:正则表达式 📗

📋 任务目标

掌握使用re模块进行正则匹配

💡 知识点回顾

课程位置:05.处理响应.pdf - 5.3 正则表达式

核心方法:

常用元字符:

📝 联系人信息

下面是一些联系人信息,请使用正则表达式提取邮箱和电话号码。

姓名 邮箱 电话
张三 13812345678
李四 13987654321
王五 15612345678

💡 示例代码

提取邮箱地址

import requests
import re
from bs4 import BeautifulSoup

url = 'https://req.haleibc.com/practice4'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# 获取所有邮箱单元格
email_cells = soup.find_all('td', class_='email-data')

# 提取邮箱
emails = []
for cell in email_cells:
    text = cell.text
    # 邮箱正则:\w+@\w+\.\w+
    email = re.findall(r'\w+@\w+\.\w+', text)
    if email:
        emails.extend(email)

print("邮箱列表:", emails)

提取电话号码

import requests
import re
from bs4 import BeautifulSoup

url = 'https://req.haleibc.com/practice4'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# 获取所有电话单元格
phone_cells = soup.find_all('td', class_='phone-data')

# 提取电话
phones = []
for cell in phone_cells:
    text = cell.text
    # 电话正则:1[3-9]\d{9}
    phone = re.findall(r'1[3-9]\d{9}', text)
    if phone:
        phones.extend(phone)

print("电话列表:", phones)

使用search方法

import re

text = "我的邮箱是zhangsan@example.com,电话是13812345678"

# 搜索邮箱
email_match = re.search(r'\w+@\w+\.\w+', text)
if email_match:
    print(f"找到邮箱:{email_match.group()}")

# 搜索电话
phone_match = re.search(r'1[3-9]\d{9}', text)
if phone_match:
    print(f"找到电话:{phone_match.group()}")

✅ 练习任务

1. 使用正则表达式提取页面中所有的邮箱地址

2. 使用正则表达式提取页面中所有的电话号码

3. 尝试使用re.match()、re.search()、re.findall()的区别

4. 练习贪婪匹配和非贪婪匹配的区别

💡 常用正则表达式

返回练习列表 下一个练习 →