【发布时间】:2020-03-18 18:04:47
【问题描述】:
我正在尝试在 powershell 中使用拆分和正则表达式解析一些日志
这是我的代码:
$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 来自 260 和 9 来自 229。试试:
$string -Split '(Test \d+:)' -
@RobertMcKee 非常感谢,它正在工作,您介意将其发布为答案并解释为什么我的正则表达式也能捕获一位数字组吗? (所以我可以接受)
-
完成,但据我所知,您的链接指向错误的正则表达式。
标签: regex powershell split