【问题标题】:regex findall pattern that doesnt contain another pattern - Python不包含其他模式的正则表达式 findall 模式 - Python
【发布时间】:2016-11-08 01:21:03
【问题描述】:

我试图从一个字符串中找到所有匹配的模式,但不包含与子字符串相同的模式。我需要做的是,找到与 <.>:.> 匹配的所有模式,在 ':' 之后没有任何嵌套标记(相同模式)。

这是输入字符串,

<First tag:Some text<Second tag:Text for second tag>Some other tag<Third tag:Text for third tag>Remaining text

预期输出,

['<Second tag:Text for second tag>','<Third tag:Text for third tag>']

再输入一个字符串,

<First tag:Some text<Second tagText for second tag>Some other tag<Third tag:Text for third tag>Remaining text

输出,

['<First tag:Some text<Second tagText for second tag>','<Third tag:Text for third tag>']

我试过这种方式

re.findall('\<[^\<.*:.*\>]+:[^\<.*:.*\>]+\>', input_string)

这在第一个示例输入中通过,但在第二个示例输入中失败。 任何建议将不胜感激:)

【问题讨论】:

    标签: regex string python-3.x match findall


    【解决方案1】:

    如果你想匹配:&lt;First tag:Some text&lt;Second tagText for second tag&gt;

    你可以试试:\&lt;[^\&lt;.*:\&gt;]+:[^.*:\&gt;]+\&gt;。 它将完成两个示例。

    见: https://regex101.com/r/nU6nO8/4 详情。

    【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多