正则匹配双引号python
时间: 2023-11-24 18:53:32 浏览: 95
python正则匹配
以下是两种Python正则匹配双引号的例子:
1. 解析双引号内容的代码
```python
import re
content = ' test "apple",a beautiful "pear" haha ' + " test 'peach' ! a big 'orange' xixi"
reg = re.compile(r'"(.*?)"')
result = re.findall(reg, content)
print('获取双引号内的内容:'+content)
print('result: ', result)
```
输出结果为:
```
获取双引号内的内容: test "apple",a beautiful "pear" haha test 'peach' ! a big 'orange' xixi
result: ['apple', 'pear']
```
2. 同时解析双单引号内容的代码
```python
import re
content = ' test "apple",a beautiful "pear" haha ' + " test 'peach' ! a big 'orange' xixi"
reg = re.compile(r"""["'](.*?)["']""")
result = re.findall(reg, content)
print('同时获取双单引号内的内容:'+content)
print('result: ', result)
```
输出结果为:
```
同时获取双单引号内的内容: test "apple",a beautiful "pear" haha test 'peach' ! a big 'orange' xixi
result: ['apple', 'pear', 'peach', 'orange']
```
阅读全文