开发者

regex negation in a pattern

开发者 https://www.devze.com 2023-01-27 02:53 出处:网络
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

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>
</m开发者_如何学Goodule>

Do anyone know how can I fix this issue?

Cheers,

Kevin


You need to use a negative lookbehind assertion.

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

Note that not all regular expression engines support lookbehind.


what about

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

精彩评论

暂无评论...
验证码 换一张
取 消