我有一個文本(不是正確的XML文檔),在XML標籤的一些話是這樣的:Python的正則表達式嵌套的XML元素
We have Potter the <term attrib="LINE:246">wizard</term> interacting with<term attrib="LINE:36080">witches</term> and <term attrib="LINE:360">goblins</term> talking about <term attrib="LINE:337"><term attrib="LINE:329"><term attrib="LINE:468">dark</term></term> <term attrib="LINE:375">arts</term></term> in regions to the east of Hogwarts.
我需要提取的XML標記的條款。 我的問題是,我不知道是什麼的正則表達式我應該用得到這樣的嵌套元素:
<term><term>something</term><term>else</term></term>
我使用python,對我的工作,我已經嘗試了我的工作如下:
re.findall(r'(<term.+?</term>)', textfile)
但我得到的是這樣的:
<term><term>something</term>
這是不好的,因爲我錯過了休息。 我也試過以下貪婪版本(這是更差):
re.findall(r'(<term.+</term>)' , textfile)
你能幫幫我嗎?
您可能會發現http://stackoverflow.com/questions/37113364/regex-for-nested-xml-attributes有關試圖解析嵌套的XML與正則表達式有關的問題的信息... –
ObZalgo:http:// stackoverflow.com/a/1732454/4014959 :) –
只有PyPi正則表達式模塊提供遞歸正則表達式。 –