python - 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>
解决方案
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>
推荐阅读
- ruby-on-rails - 如何在 ActiveAdmin 中制作可排序的列?
- python - 如何合并不同数据框的两列,如果找到匹配项,使用 pandas 在新列中写入“True”
- python - 如何使打开的stl文件不漏水
- javascript - 警告:无法对未安装的组件执行 React 状态更新
- google-secret-manager - 如何从 Google Secret Manager 解码 base64 有效负载?
- ruby-on-rails - 如何使用 Rails 5.2 中的关系属性通过关系过滤 has_many 中的记录?
- python - Beautiful Soup 内联解析和
进字典
- python - 使用 Python 消除数据文件中的特定数字
- node.js - 节点声明表达多文件的区别?
- c++ - 如何声明任何继承类的实例?