我试图匹配一个逗号分隔的key=value列表,其中的值可以很好地包含很多东西。在
我使用的模式正是来自这个related question:
split_up_pattern = re.compile(r'([^=]+)=([^=]+)(?:,|$)', re.X|re.M)
但是,当值包含html时,它会导致问题。在
下面是一个示例脚本:
import re
text = '''package_contents=<p>The basic Super 1050 machine includes the following:</p>
<p> </p>
<table style="" height: 567px;"" border=""1"">
<tbody>
<tr>
<td style=""width: 200px;"">
<ul>
<li>uper 1150 machine</li>
</ul>
</td>
<td> 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 1050 machine includes the following:</p><p> </p><table style="" height: 567px;"" border=""1""><tbody><tr><td style=""width: 200px;""><ul><li>uper 1150 machine</li></ul></td><td> With dies fitted.<ul><li>The Super 1050</li></ul></td></tr>
</tbody><table>',)
matches[1]
:
('second_attribute', 'something else')
与严格基于分隔符(逗号或等号)进行分析不同,您可以利用下一个键值对以如下方式开头的事实:
这是一个想法的草图:
^{pr2}$相关问题 更多 >
编程相关推荐