0

How can I create a regex that look for text for followed by some text and then the text { followed with some text and then the text DAO. followed with some text and then the text }, for example:

for(Entity e : list){
    e.setX(someDAO.findX(e.getId()));
}

Or :

for(Entity e : list){
   if(condition){
       someDAO.op(e.getId());
   }
}

This one didn't work for me :

\bfor\b(?s).*?DAO\.

But this will match any DAO. after a for as well as inside it, I have to be sure that I'm inside the for loop.

How can I solve this ?

Renaud is Not Bill Gates
  • 1,884
  • 24
  • 84
  • 163

2 Answers2

1

You need to make sure that the number of opening '{' is bigger than the number of closing ones '}' so that exactly one more '{' exists after the for than there are '}'.

Here is a similar question: Regular Expression to match outer brackets

The chosen answer states that this is not a regex task but there are commenters and other answers who disagree. Good luck.

Tolar
  • 21
  • 3
0

\bfor\b.*\{.*DAO\..*\} with s-flag. So in JavaScript it would be: /\bfor\b.*\{.*DAO\..*\}/s

Or without s-flag:

\bfor\b.*(\{.*[\r\n]*.*)+DAO\.(.*[\r\n]*.*\})+

Impulse The Fox
  • 2,320
  • 1
  • 23
  • 48