确定,
某些代码有些混乱。应该使用XML / DOM,而目前需要快速修复字符串上的正则表达式。我有一个元素,具有相同类型的子节点。
例如
AAA <z id="z11"> BBB <z id="z22"> CCC </z> DDD </z> endOfString.
regex中是否有一种方法可以匹配此字符串中的父节点。
<z id="z11"> BBB <z id="z22"> CCC </z> DDD </z>
是的,我知道这一切都需要重写,但请将此视为纯正的正则表达式问题。
感谢。
答案 0 :(得分:0)
好的,这肯定不是最好的方法,但你说你想要正则表达式,所以这里是正则表达式:
(<z id="[a-zA-Z0-9]+">.*?<z id="[a-zA-Z0-9]+">.*?</z>.*?</z>)
+--------------+
| Explanation: |
+--------------+
( # indicates the start of a capturing group
<z id=" # the first part of the parent-tag
[a-zA-Z0-9]+ # matches any combination of letters and numbers for the id
"> # end of the opening parent-tag
.*? # matches everything (ungreedy) up to the start of the child tag
<z id=" # the first part of the child-tag
[a-zA-Z0-9]+ # matches any combination of letters and numbers for the id
"> # end of the opening child-tag
.*? # matches everything (ungreedy) up to the closing tag
</z> # matches the closing tag (of the child)
.*? # matches everything (ungreedy) up to the closing tag
</z> # matches the closing tag (of the parent)