【发布时间】: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