匹配key=value逗号分隔列表的正则表达式,其中value可以包含html?

2024-04-26 13:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图匹配一个逗号分隔的key=value列表,其中的值可以很好地包含很多东西。在

我使用的模式正是来自这个related question

split_up_pattern = re.compile(r'([^=]+)=([^=]+)(?:,|$)', re.X|re.M)

但是,当值包含html时,它会导致问题。在

下面是一个示例脚本:

import re

text = '''package_contents=<p>The basic Super&nbsp;1050 machine includes the following:</p>
<p>&nbsp;</p>
<table style="" height: 567px;"" border=""1"">
<tbody>
<tr>
<td style=""width: 200px;"">
<ul>
<li>uper 1150 machine</li>
</ul>
</td>
<td>&nbsp;With dies fitted.
<ul>
<li>The Super 1050</li>
</ul>
</td>
</tr>
</tbody>
<table>,second_attribute=something else'''

split_up_pattern = re.compile(r'([\w_^=]+)=([^=]+)(?:,|$)', re.X|re.M)

matches = split_up_pattern.findall(text)

import ipdb; ipdb.set_trace()

print(matches)

输出:

^{pr2}$

我想要的输出是:

matches[0]

('package_contents', '<p>The basic Super&nbsp;1050 machine includes the following:</p><p>&nbsp;</p><table style="" height: 567px;"" border=""1""><tbody><tr><td style=""width: 200px;""><ul><li>uper 1150 machine</li></ul></td><td>&nbsp;With dies fitted.<ul><li>The Super 1050</li></ul></td></tr>
</tbody><table>',)

matches[1]

('second_attribute', 'something else')

Tags: therestyletablelimachineultr
1条回答
网友
1楼 · 发布于 2024-04-26 13:22:46

与严格基于分隔符(逗号或等号)进行分析不同,您可以利用下一个键值对以如下方式开头的事实:

,WORD=

这是一个想法的草图:

^{pr2}$

相关问题 更多 >