Regex for negating ends with a match

I need a regular expression to match strings that don't end with certain terms.

Input represents a group of class names such as Foo, FooImpl, FooTest, FooTestSuiteetc.

I want to match anything that doesn't end with Test, Testsor TestSuite.

Must match:

  • FooImpl
  • FooTestImpl
  • Foo

Cannot match:

  • FooTest
  • FooTestSuite
  • FooTests

I just can't get it right. What I have now is wrong, so I won’t even publish it.

+3
source share
4 answers

Try a negative lookbehind if your language supports it:

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

lookbehind, lookahead:

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

Rubular

+4

- , . - , ? (String = ~ regex)?

grep -v ( ).

+1

, grep:

grep -vE ".+(Test|Tests|TestSuite)$" *

-v - , -E - . lookaheads lookbehinds, grep , .

0

:

test\b|tests\b|testSuite\b

, .

0

Source: https://habr.com/ru/post/1772944/


All Articles