我有一个文件,我想从中解析特定的值。如何将所有以下三个正则表达式放在一起,为每个测试返回一组条目,无论是否有测量,是否有错误,如果有,还包括测量和错误?测试中可以有任意数量的测试,任意数量的测量,但测试中的单个错误没有其他测量。我尝试了许多不同的组合而没有成功。我想我需要使用前瞻和交替,但没有找到正确的组合。仅供参考,正则表达式存储在数据库中并由C#应用程序使用。提前谢谢!
输入文件:
<event>
<common>
<event_start_time>2014-01-29T17:30:36</event_start_time>
<operator>10586546</operator>
<shift>A</shift>
<program>PPM</program>
<program_revision>eo01</program_revision>
</common>
<test_instance>
<teststart startid = "ABCDEF">
<test>MB</test>
<test_start_time>2014-01-29T17:30:39</test_start_time>
<exe>HelloWorld</exe>
<subtest>CheckVersion</subtest>
<subtest_number>1</subtest_number>
</teststart>
<testend endid = "ABCDEF">
<test_result>PASS</test_result>
<test_duration duration_units="millisec">1000</test_duration>
</testend>
<teststart startid = "CDEFG">
<test>MB</test>
<test_start_time>2014-01-29T17:30:40</test_start_time>
<exe>HelloWorld</exe>
<subtest>Program1</subtest>
<subtest_number>2</subtest_number>
</teststart>
<measurement measid = "CDEFG">
<measurement_name>CycleCounter </measurement_name>
<numeric_measurement> 1</numeric_measurement>
<measurement_time>2014-01-29T17:30:50</measurement_time>
</measurement>
<measurement measid = "CDEFG">
<measurement_name>Counter </measurement_name>
<numeric_measurement> 1</numeric_measurement>
<measurement_time>2014-01-29T17:30:50</measurement_time>
</measurement>
<testend endid = "CDEFG">
<test_result>PASS</test_result>
<test_duration duration_units="millisec">10000</test_duration>
</testend>
<teststart startid = "xYZABC">
<test>MB</test>
<test_start_time>2014-01-29T17:36:01</test_start_time>
<exe>HelloWorld</exe>
<subtest>Check2</subtest>
<subtest_number>17</subtest_number>
</teststart>
<measurement measid = "xYZABC">
<measurement_name>ERROR1</measurement_name>
<error_code>31001717</error_code>
<error_message>MB:FAILED_CHECK_TEST</error_message>
<measurement_time>2014-01-29T17:36:50</measurement_time>
<measurement_result>FAIL</measurement_result>
</measurement>
<testend endid = "xYZABC">
<test_result>FAIL</test_result>
<test_duration duration_units="millisec">49000</test_duration>
</testend>
</test_instance>
<event_duration duration_units="sec">374</event_duration>
<event_result>FAIL</event_result>
要解析测试部分我正在使用正则表达式,它起作用:
\<teststart\sstartid\s=\s"
(?<tid>.*?)"\>
.*\n
.*\<test\>
(?<testid>.*?)\<
.*\n
.*\<test_start_time\>
(?<teststartdate>.*?)T
(?<teststarttime>.*?)\</.*\n
.*?
\<exe\>
(?<texe>.*?)\<.*\n
(.*?\n)*?
.*?\<testend.*?\n
.*?\<test_result\>
(?<result>.*?)\<.*\n
.*?duration_units="
(?<dunits>.{1}).*?
\>
(?<duration>.*?)\<
要解析测量数据,我使用正则表达式,它起作用:
.*?\<measurement\smeasid\s=\s"
(?<measid>.*?)"\>.*\r\n
(.*?\r\n)*?
.*?
\<measurement_name\>
(?<measurename>.*?)\<.*\r\n
.*?
\<numeric_measurement\>
(?<measurenum>[^/s].*?)\<.*\r\n
.*?
\<measurement_time\>
(?<measureDate>[^/s].*?)T
(?<measureTime>[^/s].*?)\<.*\r\n
解析错误我使用正则表达式,它起作用:
.*?\<measurement\smeasid\s=\s"
(?<measid>.*?)"\>.*\r\n
.*?\<measurement_name\>
(?<measurename>.*?)\<.*\r\n
.*?\<error_code\>
(?<sterrcode>.*?)\<.*\r\n
.*?\<error_message\>
(?<sterrmsg>.*?)\<.*\r\n
.*?\<measurement_time\>
(?<measureDate>[^/s].*?)T
(?<measureTime>[^/s].*?)\<.*\r\n
.*?\<measurement_result\>
(?<measureResult>[^/s].*?)\<.*\r\n
免责声明:是的,我知道输入是XML,但我无法将应用程序更改为反序列化,它使用正则表达式。
答案 0 :(得分:0)
您可以在零宽度断言中使用反向引用。
(?=.*?(?<foo>a))?(?=.*?(?<bar>b))?
适用于任何
ab
ba
将报告
group "foo" = "a"
group "bar" = "b"
并适用于
a
它将报告
group "foo" = "a"
group "bar" = (does not exist)
反之亦然。