Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for negation of ends with matching

I need a regex to match strings that do not end in certain terms.

Input is a bunch of Class names, like Foo, FooImpl, FooTest, FooTestSuite, etc.

I want to match anything that does not end in Test, Tests, or TestSuite.

Should Match:

  • FooImpl
  • FooTestImpl
  • Foo

Should not match:

  • FooTest
  • FooTestSuite
  • FooTests

I just can't get this right. What I have now is wrong so I won't even bother posting it.

like image 861
Jen S. Avatar asked Jan 20 '26 08:01

Jen S.


1 Answers

Try a negative lookbehind if your language supports it:

/(?<!Test)(<?!Tests)(<?!TestSuite)$/

Otherwise you can simulate a negative lookbehind using a negative lookahead:

/^(?!.*(?:Test|Tests|TestSuite)$).*$/

Rubular

like image 56
Mark Byers Avatar answered Jan 22 '26 23:01

Mark Byers