正则表达式:删除 xml 的空元素标签 [英] Regex: Remove empty-element tags for xml
本文介绍了正则表达式:删除 xml 的空元素标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将所有自闭合元素替换为长语法(因为我的网络浏览器被它们绊倒了).
I'd like to replace all self-closed elements to the long syntax (because my web-browser is tripping on them).
示例
<iframe src="http://example.com/thing"/>
变成
<iframe src="http://example.com/thing"></iframe>
我正在使用 python 风格的正则表达式.
I'm using python's flavor of regex.
推荐答案
这些解决方案都不会适应像 foo="/>" 这样的属性.试试:
None of those solutions will accommodate attributes like foo="/>". Try:
s:<([\w\-_]+)((?:[^'">]|'[^']*'|"[^"]*")*)/\s*>:<$1$2></$1>:
分解以显示细节:
<
([\w\-_]+) # tag name
(
[^'">]*| # "normal" characters, or
'[^']*'| # single-quoted string, or
"[^"]*" # double-quotes string
)*
/\s* # self-closing
>
只要标记有效,这应该始终有效.(如果你愿意,你可以使用惰性量词重新排列它;例如 '[^']' => '.*?'.)
This should always work provided that the markup is valid. (You could rearrange this using lazy quantifiers if you so chose; e.g. '[^']' => '.*?'.)
这篇关于正则表达式:删除 xml 的空元素标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文