Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to write a regex to run all unit tests but not run integration tests. Unit tests are named FooTest, integration tests are named BarIntegrationTest, with "Foo" and "Bar" being variables. I found this article on how to do it and I have solved my problem. But, its solution is to use this regex:

(.(?!Integration))*Test

I don't understand why this regex doesn't suffice:

.*(?!Integration)Test

When I tried that second regex, my Integration tests were still run.

like image 495
Daniel Kaplan Avatar asked Feb 12 '23 02:02

Daniel Kaplan


2 Answers

You're using a negative look ahead, but you want a negative look behind:

.*(?<!Integration)Test

Your regex is asserting that "Test" is not "Integration", which of course is always true.

like image 147
Bohemian Avatar answered Feb 15 '23 11:02

Bohemian


.*(?!Integration)Test

This involves .* eating up the whole string and then backtracking to match Test.For exmaple if test string is Integration test .* eats up the whole string and then applies the looahead which passes as after Test there is no integration. Once .* backtracks to match t from Test it again applies lookahead but fails so proceeds and matches Test in similar way applying the lookahead at each step.

enter image description here

like image 24
vks Avatar answered Feb 15 '23 12:02

vks