eglobetrotter
eglobetrotter

Reputation: 738

regex negation in a pattern

I'm trying to define a regex pattern with a negation within the pattern. I want to exclude all strings with 'Test' on the end. I'm aware about the character negation [^Test] but this is not what I'm looking for, [^Test] is equal to [^estT]. It should pass for strings like UserService and not for UserServiceTest. So what I did is to exclude that with {min,max}. but it doesn't work :(.

^([a-zA-Z0-9]+(Test){0,0})$

My origin idea is to put this pattern into checkstyle suppress configuration, and exclude all the Test classes from checkstyle check.

<module name="TreeWalker">
  <property name="tabWidth" value="4"/>
  <module name="TypeName">
    <property name="format" value="([a-zA-Z0-9]+(Test){0,0})"/>
  </module>
</module>

Do anyone know how can I fix this issue?

Cheers,

Kevin

Upvotes: 2

Views: 2250

Answers (2)

Bj&#246;rn
Bj&#246;rn

Reputation: 21

what about

[a-zA-Z0-9]+[^(Test)]

Upvotes: 2

robert
robert

Reputation: 34398

You need to use a negative lookbehind assertion.

^([a-zA-Z0-9]+(?<!Test))$

Note that not all regular expression engines support lookbehind.

Upvotes: 6

Related Questions