RegEx查找所有XML标签 [英] RegEx find all XML tags

查看:58
本文介绍了RegEx查找所有XML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用RegEx匹配XML文档中的所有开始标签?我只需要收集使用的标签名称即可.

How do I match all the beginning tags in an XML document with RegEx? I just need to collect the tag names used.

这就是我所拥有的:

(?<=<)(.*?)((?= \/>)|(?=>))

这与所有开始和结束标记匹配.

this matches all the beginning and closing tags.

示例:

<Habazutty>yaddayadda</Habazutty>
<Vogons />
<Targ>blahblah</Targ>

上面的代码匹配:

Habazutty
/Habazutty
Vogons
Targ
/Targ

我只需要

Habazutty
Vogons
Targ

我想不出一种方法来排除结束标记.负向前瞻无效-没有发现任何问题.我一定搞砸了.

I couldn't figure out a way to exclude the closing tags. Negative lookahead didn't work - found nothing. I must have messed up.

推荐答案

您可以将(?<=<)(.*?)((?= \/>)|(?=>))更改为(?<=<)([^\/]*?)((?= \/>)|(?=>)),即,不要使用(.*?)作为标记名,而使用([^\/]*?).标记名称中始终不允许使用/.

You could change (?<=<)(.*?)((?= \/>)|(?=>)) to (?<=<)([^\/]*?)((?= \/>)|(?=>)), i.e. instead of using (.*?) for the tag name, use ([^\/]*?). / is not allowed in tag names anyway.

这篇关于RegEx查找所有XML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆