簡體   English   中英

正則表達式,實際上應改為使用XML,將元素與子元素匹配

[英]Regex where XML should really be used instead, matching element with a child

好,

有一些代碼混亂。 暫時應該使用XML / DOM,因為現在需要使用正則表達式對字符串進行快速修復。

我有一個元素,帶有相同類型的子節點。

例如。

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

正則表達式中是否有辦法匹配此字符串中的父節點。

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

是的,我知道這一切都需要重新編寫,但是可以將其視為純正則表達式問題。

謝謝。

好的,這肯定不是最好的方法,但是您說您想要正則表達式,因此這里是用於此的正則表達式:

(<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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM