pattern = r"\{.*?\}" 改成可匹配多行
时间: 2023-07-02 12:16:33 浏览: 113
如果您想要匹配多行中的大括号,可以使用正则表达式中的 `re.DOTALL` 标志来启用点号(`.`)匹配换行符的功能。具体来说,您可以这样修改您的正则表达式:
```python
import re
text = """
This is a {sample} text with {multiple} pairs of {braces}.
Here is another line with {more} {braces}.
And this is a third line with {even} {more}
{braces}.
"""
pattern = r"\{.*?\}"
matches = re.findall(pattern, text, flags=re.DOTALL)
print(matches)
```
在上面的代码中,我们使用了三个连续的双引号来定义一个多行字符串 `text`,其中包含多个大括号对。我们将正则表达式模式 `r"\{.*?\}"` 传递给 `re.findall()` 函数,并在 `flags` 参数中设置 `re.DOTALL` 标志。这个标志告诉 Python 在匹配文本时,将点号(`.`)也包括在匹配范围内,从而使它可以匹配换行符。
运行上面的代码,结果如下所示:
```
['{sample}', '{multiple}', '{braces}', '{more}', '{braces}', '{even} {more}\n{braces}']
```
以上结果中,`re.findall()` 函数找到了所有匹配正则表达式模式的子字符串,并将它们存储在一个列表中。请注意,最后一个大括号对 `{even} {more}\n{braces}` 包括了一个换行符,因为我们已经启用了 `re.DOTALL` 标志。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import os from bs4 import BeautifulSoup import re # 指定文件夹路径 folder_path = "C:/Users/test/Desktop/DIDItest" # 正则表达式模式 pattern = r'<body>(.*?)<\/body>' # 遍历文件夹中的所有文件 for root, dirs, files in os.walk(folder_path): for file in files: # 读取html文件 file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8") as f: html_code = f.read() # 使用正则表达式匹配<body>标签内的数据 body_data = re.findall(pattern, html_code, re.DOTALL) # 剔除和() body_data = body_data[0].replace("", "").replace("()", "") # 使用正则表达式提取talk_id、时间、发送者ID和接收者ID matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词 matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)<.*?>', body_data) # match = re.search('(中发言|发送)\s(.*?)\s', body_data) # if match: # content = match.group(2) matches2 = re.findall('(中发言|发送)\s(.*?)\s', body_data) for match in matches2: content = match[1] soup = BeautifulSoup(content, 'html.parser') if soup.find('= 2: receive_id = matches1[3] # 处理匹配结果 for match in matches: talk_id = match[0] time = match[1] send_id = match[2] talk_type = match[3] # 进行时间格式转换,将time转换为"0000-00-00"格式 time = time.replace('年', '-').replace('月', '-').replace('日', '') talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊') # 打印结果 print("Talk ID:", talk_id) print("Time:", time) print("Sender ID:", send_id) print("Receive_id:", receive_id) print("Talk_type:", talk_type) print("Content:",content) print("---")导入至csv
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)