上传文件至 /
This commit is contained in:
13
text1.py
Normal file
13
text1.py
Normal file
@@ -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')
|
||||||
33
text2.py
Normal file
33
text2.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
html = '''
|
||||||
|
<div class="movie">
|
||||||
|
<h2 class="name">《流浪地球》</h2>
|
||||||
|
<span class="year">(2024)</span>
|
||||||
|
<span class="rating">8.5</span>
|
||||||
|
<span class="director">导演:郭帆</span>
|
||||||
|
</div>
|
||||||
|
<div class="movie">
|
||||||
|
<h2 class="name">《你好,李焕英》</h2>
|
||||||
|
<span class="year">(2024)</span>
|
||||||
|
<span class="rating">7.9</span>
|
||||||
|
<span class="director">导演:贾玲</span>
|
||||||
|
</div>
|
||||||
|
'''
|
||||||
|
|
||||||
|
# 编写正则表达式,提取所有电影信息
|
||||||
|
# pattern = r'你的正则表达式'
|
||||||
|
|
||||||
|
# 提示:可以用多个正则分别提取,或者用一个复杂的正则提取所有
|
||||||
|
name_pattern = r'<h2 class="name">《([^》]+)》</h2>'
|
||||||
|
year_pattern = r'<span class="year">\((\d{4})\)</span>'
|
||||||
|
rating_pattern = r'<span class="rating">([^<]+)</span>'
|
||||||
|
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]}")
|
||||||
15
text3.py
Normal file
15
text3.py
Normal file
@@ -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}')
|
||||||
18
text4.py
Normal file
18
text4.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user