区分/<来自<在python中

时间:2016-07-06 09:03:14

标签: regex python-3.x backslash findall

我有一个模式可以找到这种格式的标签,<。*:。*> 。 从嵌套标签,我只采用子标签。 在这里,我需要将括号(<和>)与'/<'区分开来和'/>'。 有什么方法可以用同样的模式来做到这一点吗?

例如:输入字符串

<testing this> any text </<this is not a tag>any text<this will fail/>>

输出:

['<testing this>','</<this is not a tag>','<this will fail/>>']

任何建议请告诉我。

1 个答案:

答案 0 :(得分:0)

使用此模式:

(?<!/)<.*?(?<!/)>

(?<!/)是一个负面的背后隐藏,可确保每个<>的左侧没有斜线,而不会实际消耗该角色。

Check this pattern out on regex101.com