我有以下来自Protractor测试的输出,我正在尝试解析并找到失败规范的名称:
Specs: /var/lib/jenkins-slave/workspace//tests/js/e2e/ui_e2e/tests/test_one.js
Selenium URL: http://10.20.40.54:4444/wd/hub
..... number of different strings here....
Executed 25 of 25 specs SUCCESS in 1 min 23 secs.
-----
Specs: /var/lib/jenkins-slave/workspace//tests/js/e2e/ui_e2e/tests/test_two.js
..... number of different strings here....
UnknownError: Due to a previous error, this job has already finished. You can learn more at
我正在使用以下正则表达式:
(?:Specs:)(?:[\s\S])*(?=UnknownError(.*?))
但是,它匹配第一个字符串过宽的部分,吞噬字符串test_two.js
。
如何避免吞咽和缩小匹配:
Specs: /var/lib/jenkins-slave/workspace//tests/js/e2e/ui_e2e/tests/test_two.js
..... number of different strings here....
UnknownError: Due to a previous error, this job has already finished. You can learn more at
答案 0 :(得分:2)
我认为你不是在寻找一个“最近的”匹配,而是寻找开始和结束分隔符之间最短的“窗口”。
您可以使用
/^Specs:(?:(?!^Specs:)[\s\S])*UnknownError:\s*(.*)/gm
或其效率更高的展开变体:
/^Specs:.*(?:\r?\n(?!Specs:).*)*UnknownError:\s*(.*)/gm
请参阅regex demo
<强>详情:
^
- 开始行Specs:
- 一系列文字字符(?:(?!^Specs:)[\s\S])*
- 一个调和的贪婪令牌,匹配任何不启动行开头序列的字符Specs:
(在展开的变体中,与.*(?:\r?\n(?!Specs:).*)*
子模式完成相同) UnknownError:
- 子字符串\s*
- 0+ whitespaces (.*)
- 第1组捕获除换行符之外的任何零个或多个字符。