【发布时间】:2017-06-13 14:44:12
【问题描述】:
我有以下文字:
<clipPath id="p54dfe3d8fa">
<path d="M 112.176 307.8
L 112.176 307.8
L 174.672 270
L 241.632 171.72
L 304.128 58.32
L 380.016 171.72
L 442.512 217.08
L 491.616 141.48
L 491.616 307.8
z
"/>
</clipPath>
<clipPath id="p27c84a8b3c">
<rect height="302.4" width="446.4" x="72.0" y="43.2"/>
</clipPath>
我需要把这部分拿出来:
d="M 112.176 307.8
L 112.176 307.8
L 174.672 270
L 241.632 171.72
L 304.128 58.32
L 380.016 171.72
L 442.512 217.08
L 491.616 141.48
L 491.616 307.8
z
"
我需要将此部分替换为其他内容。我能够获取整个 <clipPath ...><path d="[code i want]"/> 但这对我没有帮助,因为我无法覆盖 <clipPath> 元素中的 id。
请注意,我不想触及其他<clipPath> 元素。我只想更改<clipPath> 元素中的<path> 元素。
我认为答案与选择 clipPath 元素之前的所有内容并在 Path 部分结束有关。任何帮助将不胜感激。
我一直在使用http://pythex.org/ 寻求帮助,并且还看到了奇怪的行为(与多行和空格有关),这些行为与 python 3.x 代码之间的行为不同。
以下是我尝试过的一些事情:
reg = r'(<clipPath.* id=".*".*>)'
reg = re.compile(r'(<clipPath.* id=".*".*>\s*<path.*d="(.*\n)+")')
reg = re.compile(r'((?<!<clipPath).* id=".*".*>\s*<path.*d="(.*\n)+")')
g = reg.search(text)
g
【问题讨论】:
-
clipPaths 可以嵌套吗? -
不,我不这么认为。
-
你为什么要用正则表达式?
-
这是
xml吗?你为什么不用xml.etree.ElementTree或lxml来做这个?