如何为此方案编写正则表达式模式

时间:2012-07-19 10:34:34

标签: regex

我试图在下面的示例xml中找到特殊字符外观。

<?xml version="1.0"?>
<PayLoad>
<requestRows>****</requestRows>
<requestRowLength>1272</requestRowLength>
<exceptionTimestamp>2012070202281068-0700</exceptionTimestamp>
<exceptionTimestamp>201$2070202281068-0700</exceptionTimestamp>
<exceptionTimestamp>20120(702022810680700</exceptionTimestamp>
<exceptionDetail>NO DATA AVAILABLE FOR TIME PERIOD SPECIFIED   =</exceptionDetail>
</PayLoad>

我必须找到一个包含$,(,=, - 字符的整个标签。对于这个我写在正则表达式模式下面 (<[\w\d]*>\w*(?<value>[^\w]+)\w*\d*</[\w\d]*>) 并返回以下输出(在Expresso Tool中运行)

<requestRows>****</requestRows>
<exceptionTimestamp>2012070202281068-0700</exceptionTimestamp>
<exceptionTimestamp>20120(702022810680700</exceptionTimestamp>

但它也应该回到两个以下的enrty。

<exceptionTimestamp>201$2070202281068-0700</exceptionTimestamp>
<exceptionDetail>NO DATA AVAILABLE FOR TIME PERIOD SPECIFIED   =</exceptionDetail>

省略这些条目,因为它包含多个特殊字符(包括空格)。任何人都可以请给我一个正确的正则表达式为上述场景。 感谢。

1 个答案:

答案 0 :(得分:1)

我会在中间部分使用环视,而不是

(<[\w\d]*>\w*(?<value>[^\w]+)\w*\d*</[\w\d]*>)

我会用

(<[\w\d]*>(?=[^<]*[^<\w])(?<value>.*)</[\w\d]*>)

如果我没有真正认识到语法的?<value>部分,那就变成了

(<[\w\d]*>(?=[^<]*[^<\w]).*</[\w\d]*>)

如果您想特别保存任何内容,只需添加捕获组即可。