JSBach
JSBach

Reputation: 4747

Find string matching pattern1 but not matching pattern 2

I am trying to work on a piece of code that will refactor my system. So I will read all my classes and find every class or object that is called Manager using regex. I want to do that only for the classes I wrote, so I do not want to find BeanManager and EntityManager classes.

Currently my regex is

/([a-zA-Z]*)Manager/

This works nice, but BeanManager and EntityManager are also included.

I've found this kind of question: Regular expression to match a line that doesn't contain a word?. In this case the OP wanted to find anything that doesn't match a pattern, but in my case I would like to find everything matching a pattern except if it matches a second pattern

Is there any way I can do that?

Sorry, I forgot the examples

I would like to include things like

MyManager

myManager

ClientManager

clientManager

testManager

TestManager

but exclude

BeanManager

EntityManager

Upvotes: 1

Views: 434

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627056

Use word boundaries with a negative look-ahead to exclude the compounds that you want to ignore:

\b(?!Bean|Entity)([a-zA-Z]*)Manager\b

See demo

Upvotes: 2

Related Questions