2013-09-27 144 views
-1

我是正則表達式的學習者。我正試圖從下面的字符串中找到日期。 元素<ext:serviceitem>可以在實際的xml中重複多達20次。我只需要取出日期字符串(就像名稱中以Date結尾的任何元素,我需要該元素的值是日期)。例如和。我希望所有這些日期(只)被打印出來。重複序列的正則表達式

<ext:serviceitem><ext:name>EnhancedSupport</ext:name><ext:serviceItemData><ext:serviceItemAttribute name="Name">E69D7F93-81F4-09E2-E043-9D3226AD8E1D-1</ext:serviceItemAttribute><ext:serviceItemAttribute name="ProductionDatabase">P1APRD</ext:serviceItemAttribute><ext:serviceItemAttribute name="SupportType">Monthly</ext:serviceItemAttribute><ext:serviceItemAttribute name="Environment">DV1</ext:serviceItemAttribute><ext:serviceItemAttribute name="StartDate">2013-11-04 10:02</ext:serviceItemAttribute><ext:serviceItemAttribute name="EndDate">2013-11-12 10:02</ext:serviceItemAttribute><ext:serviceItemAttribute name="No_of_WeeksSupported"></ext:serviceItemAttribute><ext:serviceItemAttribute name="Cost"></ext:serviceItemAttribute><ext:serviceItemAttribute name="SupportNotes"></ext:serviceItemAttribute><ext:serviceItemAttribute name="FiscalQuarterNumber"></ext:serviceItemAttribute><ext:subscription><ext:loginID>kbasavar</ext:loginID><ext:ouname>020072748</ext:ouname></ext:subscription></ext:serviceItemData></ext:serviceitem><ext:serviceitem><ext:name>EnhancedSupport</ext:name><ext:serviceItemData><ext:serviceItemAttribute name="Name">E69D7F93-81F4-09E2-E043-9D3226AD8E1D-2</ext:serviceItemAttribute><ext:serviceItemAttribute name="ProductionDatabase">P1BPRD</ext:serviceItemAttribute><ext:serviceItemAttribute name="SupportType">Quarterly</ext:serviceItemAttribute><ext:serviceItemAttribute name="Environment">TS2</ext:serviceItemAttribute><ext:serviceItemAttribute name="StartDate">2013-11-11 10:03</ext:serviceItemAttribute><ext:serviceItemAttribute name="EndDate">2013-11-28 10:03</ext:serviceItemAttribute><ext:serviceItemAttribute name="No_of_WeeksSupported"></ext:serviceItemAttribute><ext:serviceItemAttribute name="Cost"></ext:serviceItemAttribute><ext:serviceItemAttribute name="SupportNotes"></ext:serviceItemAttribute><ext:serviceItemAttribute name="FiscalQuarterNumber"></ext:serviceItemAttribute><ext:subscription><ext:loginID>kbasavar</ext:loginID><ext:ouname>020072748</ext:ouname></ext:subscription></ext:serviceItemData></ext:serviceitem> 

我試着用下面的正則表達式,但第一次出現後的字符串返回其休息。

(?<=Date\"\>).*(?=\<\/ext\:serviceItemAttribute\>) 

任何幫助將不勝感激。

+2

匹配日期看一看[此](http://stackoverflow.com/questions/8577060/why-it-it-it-a-bad-idea-to-parse-xml-with-regex)。 –

回答

0

您的問題是.*是貪婪的,這意味着它將從Date的第一個實例抓取到</ext:ser....的最後一個實例。將.*替換爲.*?,它會改變你的行爲。

#(?<=Date">).*?(?=</ext:serviceItemAttribute>)#i 

你應該有一個捕獲組.*?(.*?)

#(?<=Date">)(.*?)(?=</ext:serviceItemAttribute>)#i 

你也可以做到這一點 - 更簡單 - 樣:

#Date">(.*?)</ext#i 

更新

正如已指出了下面這個註釋(上圖)的解決方案依賴於使用非貪婪匹配。

要解決這個問題,你可以使用以下命令:([^<]*)代替(.*?)

注:這不會影響下面的替代品。


替代

/(\d{4}-\d{2}-\d{2})/ 
/(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/ 

上述圖案將在格式和YYYY-XX-XX分別YYYY-XX-XX HH:MM

+0

這當然假設您的正則表達式方言支持非貪婪匹配。 OP最好包含有關平臺的信息,因此我們不必猜測可用工具支持哪些正則表達式功能。 – tripleee

+0

非常感謝。這個'(?<=Date">)(。*?)(?=)'爲我工作。 – Kiran

+0

很高興知道它的工作! @tripleee:一個有效的觀點,事實證明它在這種情況下起作用。不過,我已經用解決方法更新了答案。 – Steven