1

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

Community
  • 1
  • 1
JSBach
  • 4,679
  • 8
  • 51
  • 98
  • Please include some example of what you want to match, and what you would not like to match. – npinti May 18 '15 at 13:34

1 Answers1

2

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • In most examples here on SO, you will see the use of `^` and `$` anchors, but `\b` is a good replacement when it comes to anchoring a match inside a larger string. Glad it helps. – Wiktor Stribiżew May 18 '15 at 13:40