我正在尝试在Powershell中使用split和regexes解析一些日志
这是我的代码:
$string = "Starting ChromeDriver 78.0.3904.70Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. Test 229: Passed Test 260: Failed. Error message: Status: Test case failed. Steps: Navigate to: PurchReqTableListPage (purchreqpreparedbyme) Use the Quick Filter to find records. For example, filter on the Purchase requisition fION()</StackTrace> </Error> Playback results: Tests: 2 Passed: 1 Failed: 1"
$string -Split '(Test (\d)+:)'
结果:
Starting ChromeDriver 78.0.3904.70Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Test 229:
9
Passed
Test 260:
0
Failed. Error message: Status: Test case failed. Steps: Navigate to: PurchReqTableListPage (purchreqpreparedbyme) Use the Quick Filter to find records. For example, filter on the Purchase requisition fION()</StackTrace> </Error> Playback results: Tests: 2 Passed: 1 Failed: 1
预期结果:
Starting ChromeDriver 78.0.3904.70Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Test 229:
Passed
Test 260:
Failed. Error message: Status: Test case failed. Steps: Navigate to: PurchReqTableListPage (purchreqpreparedbyme) Use the Quick Filter to find records. For example, filter on the Purchase requisition fION()</StackTrace> </Error> Playback results: Tests: 2 Passed: 1 Failed: 1
在此站点上:https://regexr.com/3c0lf我尝试了此正则表达式,捕获的组为:Test 260:
和Test 229:
(正是我想要的)
我不知道0
和9
的来源。
非常感谢
答案 0 :(得分:2)
这些是数字的最后数字。 26 * 0 *为0,22 * 9 *为9。 之所以会看到这些,是因为您通过在数字周围加上括号来创建了另一个捕获组。像这样删除它们:
$string -Split '(Test \d+:)
您可能甚至也不需要这些括号,只留下
$string -Split 'Test \d+: