2013-09-27 145 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