python - 非贪婪 XML 中的多个匹配(Python 正则表达式)

标签 python regex xml greedy

我知道这个话题被很多人问到,但我找不到问题的答案:

在附图中,有许多不同的缓冲区,我希望仅匹配名称中包含“Lut”的缓冲区(请注意,图像中的字符串中有 2 个匹配项)。我遇到的问题是匹配项还包含我想要的缓冲区之前的缓冲区。

/image/p0fPS.png

我对正则表达式还很陌生,并且仍在努力学习,因此任何解释将不胜感激。

谢谢! :)

系上绳子是为了让您感到舒适(如果需要):

<?xml version="1.0" encoding="utf-8"?>
<pimp xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <dllPath>C:\ReplayCode\Apps\Pimp</dllPath>
    <buffers>   
    <buffer name="InputMask">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="MaskErode">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="BlablaLutBla">
            <width>256</width>
            <height>256</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="MaskClose">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="InputVis">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>3</channels>
            <type>IMG</type>
    </buffer>   
        <buffer name="AddMaskEdge">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="EdgeVis">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>3</channels>
            <type>IMG</type>
    </buffer>       
        <buffer name="GrayEdge">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="EdgeMaskMulThreshold">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>
    <buffer name="MaskMulEdge">
            <width>5120</width>
            <height>3072</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>   
    </buffers>  

我尝试的正则表达式是这样的:

<buffer name=".*?Lut.*?">.*?<\/buffer>

我预计有 2 场比赛:

<buffer name="BlablaLutBla">
            <width>256</width>
            <height>256</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>

<buffer name="2ndLutBlabla">
            <width>256</width>
            <height>256</height>
            <data>UCHAR</data>
            <channels>1</channels>
            <type>IMG</type>
    </buffer>

最佳答案

您可以使用BeautifulSoup来解析您的标签。

import re
from bs4 import BeautifulSoup

input_xml = ''' some xml '''
soup = BeautifulSoup(input_xml, "lxml-xml")
print soup.find_all('buffer', attrs={"name": re.compile('Lut')})

如果您尚未安装此程序:

pip install beautifulsoup4
pip install lxml

关于python - 非贪婪 XML 中的多个匹配(Python 正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839172/

相关文章:

xml - JAXB 将从 XSD 创建映射

java - 消息 : Invalid byte 1 of 1-byte UTF-8 sequence in hadoop

Python,将 9 个元组 UTC 日期转换为 MySQL 日期时间格式

python - 解释 “Traceback (most recent call last):”错误

c# - 正则表达式 - 最长的可能值

regex - 如何使用 Notepad++ 正则表达式搜索匹配 HTML 属性?

Python zipfile 模块不压缩文件

python - 从 JPG 到 b64encode 再到 cv2.imread()

javascript - 如何不允许仅设置相同字符和特殊符号 - regex js

java - 使用 Sax 解析器的 RSS 阅读器丢失标题中的字符