From 304a2c3f5189bffd8ca1ea6c69314c0e9424c8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E4=BC=A0=E6=95=8F?= <2509165031@student.example.com> Date: Thu, 2 Apr 2026 16:07:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- text1.py | 13 +++++++++++++ text2.py | 33 +++++++++++++++++++++++++++++++++ text3.py | 15 +++++++++++++++ text4.py | 18 ++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 text1.py create mode 100644 text2.py create mode 100644 text3.py create mode 100644 text4.py diff --git a/text1.py b/text1.py new file mode 100644 index 0000000..8672b41 --- /dev/null +++ b/text1.py @@ -0,0 +1,13 @@ +import re +text = ''' +2024-03-15 天气:晴 温度:15-25°C +2024-03-16 天气:多云 温度:12-20°C +2024-03-17 天气:小雨 温度:10-18°C +''' + +pattern = r'(\d{4}-\d{2}-\d{2})\s*天气:([^ ]+)\s*温度:(\d+)-(\d+)°C' +matches = re.findall(pattern, text) + +for match in matches: + date, weather, low, high = match + print(f'{date}: {weather}, {low}°C-{high}°C') \ No newline at end of file diff --git a/text2.py b/text2.py new file mode 100644 index 0000000..0c38497 --- /dev/null +++ b/text2.py @@ -0,0 +1,33 @@ +import re + +html = ''' +
+

《流浪地球》

+ (2024) + 8.5 + 导演:郭帆 +
+
+

《你好,李焕英》

+ (2024) + 7.9 + 导演:贾玲 +
+''' + +# 编写正则表达式,提取所有电影信息 +# pattern = r'你的正则表达式' + +# 提示:可以用多个正则分别提取,或者用一个复杂的正则提取所有 +name_pattern = r'

《([^》]+)》

' +year_pattern = r'\((\d{4})\)' +rating_pattern = r'([^<]+)' +director_pattern = r'导演:([^<]+)' + +names = re.findall(name_pattern, html) +years = re.findall(year_pattern, html) +ratings = re.findall(rating_pattern, html) +directors = re.findall(director_pattern, html) + +for i in range(len(names)): + print(f"{names[i]} | {years[i]} | 评分:{ratings[i]} | {directors[i]}") \ No newline at end of file diff --git a/text3.py b/text3.py new file mode 100644 index 0000000..2158475 --- /dev/null +++ b/text3.py @@ -0,0 +1,15 @@ +import re + +log = ''' +192.168.1.100 - - [15/Mar/2024:10:15:30 +0800] "GET /index.html HTTP/1.1" 200 1234 +10.0.0.50 - - [15/Mar/2024:10:15:31 +0800] "POST /api/login HTTP/1.1" 200 256 +192.168.1.101 - - [15/Mar/2024:10:15:32 +0800] "GET /notfound.html HTTP/1.1" 404 512 +172.16.0.200 - - [15/Mar/2024:10:15:33 +0800] "GET /images/logo.png HTTP/1.1" 200 4096 +''' + +# 提取 IP、时间和状态码 +pattern = r'(\d+\.\d+\.\d+\.\d+).*?\[([^\]]+)\].*?" (\d{3}) \d+' + +for match in re.finditer(pattern, log): + ip, time, status = match.groups() + print(f'IP: {ip:15} | 时间: {time:25} | 状态: {status}') \ No newline at end of file diff --git a/text4.py b/text4.py new file mode 100644 index 0000000..e97cb05 --- /dev/null +++ b/text4.py @@ -0,0 +1,18 @@ +import re + +phone_book = ''' +张三:138-1234-5678 +李四:139-5678-1234 +王五:138-0000-1111 +''' + +# 脱敏:将 138-****-5678 格式输出 +# 提示:使用分组和 re.sub + +pattern = r'(\d{3})-(\d{4})-(\d{4})' + +def mask_phone(match): + return f'{match.group(1)}-****-{match.group(3)}' + +masked = re.sub(pattern, mask_phone, phone_book) +print(masked) \ No newline at end of file