Question

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

Was it helpful?

Solution

You need to use a negative lookbehind assertion.

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

Note that not all regular expression engines support lookbehind.

OTHER TIPS

what about

[a-zA-Z0-9]+[^(Test)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top