python - 匹配 "without this"

标签 python regex

我需要删除所有<p></p>只有<p>位于<td> .
但如何才能做到呢?

import re
text = """
    <td><p>111</p></td>
    <td><p>111</p><p>222</p></td>
    """
text = re.sub(r'<td><p>(??no</p>inside??)</p></td>', r'<td>\1</td>', text)

如何匹配without</p>inside

最佳答案

我会使用minidom 。我从 here 窃取了以下代码片段您应该能够对其进行修改并为您工作:

from xml.dom import minidom

doc = minidom.parse(myXmlFile)
for element in doc.getElementsByTagName('MyElementName'):
    if element.getAttribute('name') in ['AttrName1', 'AttrName2']:
        parentNode = element.parentNode
        parentNode.insertBefore(doc.createComment(element.toxml()), element)
        parentNode.removeChild(element)
f = open(myXmlFile, "w")
f.write(doc.toxml())
f.close()

谢谢@Ivo Bosticky

关于python - 匹配 "without this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652266/

相关文章:

python - 如何唯一地组合2个列表

python - Bool对象不支持item赋值

Javascript反向匹配过程

regex - 我的正则表达式有什么问题?

javascript - "match is null"仅在服务器 - jQuery 1.x

python - 在网络浏览器中嵌入本地 Jupyter 笔记本单元

python |如何将元素随机添加到列表中

python - 回归结果不正确

java - 如何提取字符串的重复部分?

regex - 使用 find 或 grep 查找来自不同编码系统(Windows 到 Linux)的带有重音字符的文件名