0

I searched around for a similar question but could not find one.

Inside Visual Studio in my code files, I want to find all the foreach loops that contain a particular string of code? I can easily do a find all foreach but that turns up hundreds and it would be too hard to double click each on to do a manual inspection?

eg.

foreach(var foo in foos)
{
Console.WriteLine("I need search for the string 'Console' inside the foreach.");
}

I thought of regular expressions but I have no idea how to write that. Not sure if there is another way.

1 Answer 1

0

Hmm... I guess a regex would be a good way to go. I am not a visual studio user but here's a regex you could use:

foreach\([^\)]+\)[^}]+(YOUR SEARCH STRING HERE)[^}]+}
3
  • I tried it but Visual Studio gave me back the following error: Empty character set [] or [^] was specified. I am not good at regex so not sure what it means. May 10, 2012 at 23:15
  • maybe this will work, I don't know if } is a special character... foreach([^)]+)[^\}]+(YOUR SEARCH STRING HERE)[^\}]+} May 11, 2012 at 1:37
  • Thank you for your help but I it still not working. VS is telling me there is a Syntax error in pattern now. Not very helpful is it. May 11, 2012 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.