Regex for java:S3578

Hi there, I would like to change that Regex as follows:

“Check that the Method starts with test or ends with Test

How would the fitting Regex look like?


Where am i coming from?:

Rule java:S3578 checks the convention of starting a Test-Method with “test

like in:

testMethodSomething()

The supplied original Regex ist as follows: ^test[A-Z][a-zA-Z0-9]*$

TIA very much!
Daniel

Hey @daniel

A tool like https://regex101.com/ will probably help you.

It’s probably not the most graceful RegEx – but simply splitting into two capturing groups and providing a logical OR will probably get you close.

(^test[A-Z][a-zA-Z0-9]*$)|(^[A-Z][a-zA-Z0-9]*Test$)

1 Like

that was quiiiick! … the capturing groups might do the trick. i am currently in regex101 playing around.

have tried it with the following but was on the wrong path: [^test[A-Z][a-zA-Z0-9]*$|^[a-z][a-zA-Z0-9]*Test$]

in the second group the first letter needs to be lowercase :nerd_face: … if you edit your answer i will mark it(as the answer), or i would insert the solution myself

//another edit: how did you create the shortlink to your regex101-example, i wanted to do the same but failed to create it

Regex that checks that a methodname starts with test or ends with Test:

(^test[A-Z][a-zA-Z0-9]*$)|(^[a-z][a-zA-Z0-9]*Test$)


(it is a slight variation on colins fine reply … a methodname ending with *Test should start lowercase)
Thank you for the quick help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.