Home > Software design >  Negative look around matching when it shouldn't
Negative look around matching when it shouldn't

Time:01-02

I am trying to understand regex better, along with lookarounds in this tester.

I have the following regex:

class.*(?!(XC))

And the following test:

class LocalCardsTests: XCTestCase { // should not match
class LocalTest2 { // Should match

However, both lines match when only one should. What am I missing?

CodePudding user response:

The problem is the .* should in the lookahead:

class(?!.*XC)

Your regex class.*(?!(XC)) (which btw has unnecessary brackets) matches "class" and all remaining characters, because (?!XC) is always true at the end of input.

  • Related