为什么.*(?!Integration)Test 匹配 FooIntegrationTest?

Why does .*(?!Integration)Test match FooIntegrationTest?

我正在尝试为 运行 所有单元测试而不是 运行 集成测试编写正则表达式。单元测试命名为FooTest,集成测试命名为BarIntegrationTest,"Foo"和"Bar"为变量。 I found this article on how to do it and I have solved my problem。但是,它的解决方案是使用这个正则表达式:

(.(?!Integration))*Test

我不明白为什么这个正则表达式不够用:

.*(?!Integration)Test

当我尝试第二个正则表达式时,我的集成测试仍然是 运行。

你在前面使用负面形象,但你想要负面形象后面:

.*(?<!Integration)Test

您的正则表达式断言 "Test" 不是 "Integration",这当然总是正确的。

.*(?!Integration)Test

这涉及 .* 吃掉整个字符串然后回溯以匹配 Test。例如,如果测试字符串是 Integration test .* 吃掉整个字符串并且然后应用在 Test 之后没有 integration 的循环。 一旦 .*Test 回溯到匹配 t 它再次应用前瞻但失败所以继续并以类似的方式匹配测试在每个步骤应用前瞻。