0

Ok,

there is a mess with some code. XML/DOM should have been used, instead, for the time being a quick fix with regex on strings is required.

I have an element, with a child node of the same type.

eg.

AAA <z id="z11"> BBB <z id="z22"> CCC </z> DDD </z> endOfString.

Is there a way in regex to match the parent node in this string.

<z id="z11"> BBB <z id="z22"> CCC </z> DDD </z>

Yes, I know this all needs to be re-written, but think of this as a pure regex question.

Thanks.

4

1 回答 1

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)
于 2013-06-03T10:58:38.227 回答