直接拼接的
时间: 2023-07-02 07:12:15 浏览: 47
好的,那我可以通过正则表达式来匹配出其中的图片 base64 和文本部分。下面是一个可以实现该功能的 Python 代码示例:
```python
import re
# 假设这是你提供的混合了图片 base64 和文本的内容
mixed_content = '这是一段混合了图片和文本的内容,图片如下:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA...这是图片下面的文本部分'
# 匹配图片 base64 部分的正则表达式
img_regex = r'data:image/.*?;base64,(.*?)\s'
# 匹配文本部分的正则表达式
text_regex = r'^.*?(?=data:image/|$)'
# 从混合内容中取出图片 base64 和文本部分
img_base64 = re.findall(img_regex, mixed_content)
text = re.findall(text_regex, mixed_content, re.DOTALL | re.MULTILINE)[0].strip()
# 打印结果
print('图片 base64: ', img_base64)
print('文本: ', text)
```
需要说明的是,上述代码中使用了两个正则表达式,分别是 `img_regex` 和 `text_regex`。其中,`img_regex` 匹配以 `data:image/` 开头、以 `;base64,` 结尾的部分,这就是图片的 base64 编码部分。而 `text_regex` 匹配的则是 `data:image/` 和字符串结尾之间的所有部分,这就是文本部分。为了确保能够匹配到多行文本,`text_regex` 使用了 `re.DOTALL` 和 `re.MULTILINE` 两个标志位。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)