首页 > 解决方案 > Using regular expression to replace xml in Python

问题描述

I want to convert the .mdx file to the .dictionary used by the MAC. So, I need to read and replace many lines in a .xml file. But my problem is there are so many different lines to replace. This is some of the xml file that need to be replaced:</p>

<p>@@@LINK=ten pence</p>
<p>@@@LINK=twenty-twenty vision</p>
<p>@@@LINK=fifty pence</p>
<p>@@@LINK=abate</p>

And it will become:

<a href="x-dictionary:d:ten pence:dict_bundle_id">ten pence</a>
<a href="x-dictionary:d:twenty-twenty vision:dict_bundle_id">twenty-twenty vision</a>
<a href="x-dictionary:d:fifty pence:dict_bundle_id">fifty pence</a>
<a href="x-dictionary:d:abate:dict_bundle_id">abate</a>

标签: pythonregex

解决方案


Capture everything between the = and the </p> in the first group with

<p>@@@LINK=(.+?)</p>

and then you can replace with the desired format via

<a href="x-dictionary:d:\g<1>:dict_bundle_id">\g<1></a>

https://regex101.com/r/zOKlW8/2


推荐阅读