2014-01-21 33 views
-2

如何通過Regex搜索xml文件並在Delphi中獲取特定的屬性值?使用Regexp進行搜索XML

例如在此xml:

<?xml version=」1.0」 encoding=」UTF-8」 ?> 
<School> 
    <Class> 
     <StudentID> 1 </StudentID> 
     <StudentName> Joe </StudentName> 
     <StudentFamily> Brown </StudentFamily> 
    </Class> 
    <Class> 
     <StudentID> 2 </StudentID> 
     <StudentName> Michel </StudentName> 
     <StudentFamily> Adams </StudentFamily> 
    </Class> 
    <Class> 
     <StudentID> 3 </StudentID> 
     <StudentName> Joel </StudentName> 
     <StudentFamily> Thompson </StudentFamily> 
    </Class> 

</School> 

也就是說,搜索「喬*」(在StudentName屬性)的代碼應該返回「布朗」和「湯普森」(從StudentFamily屬性)。

我可以在簡單情況下使用FindNode函數來獲取xmlDocument,但我必須使用Regex。

任何想法是值得歡迎的。

+0

您寫的正則表達式有任何語言限制嗎?另外,是否有一個原因,你不能只使用xpath這個? – SuperFamousGuy

+0

我推薦使用Xpath。這將更加可行。 – Innovation

+3

此問題已經由bobince回答:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –

回答

0

如何: (?:\<StudentName\> Joe.* \<\/StudentName\>[\s]*<StudentFamily>\s*)(\w*) 當然,你必須建立的模式,並把 「喬*」 作爲在正確的地方

0

嘗試 「喬*」:

(?:<StudentName>\sjoe[^< ]*).*?\n.*?<StudentFamily>([^<]*) 

Demo

說明: enter image description here