1

I have lots of code blocks like:

try
{
 ...
}
catch(Exception123 &e)
{
 ...
}

I want to replace them with something like this:

MY_MACRO(try
{
 ...
})
catch(Exception123 &e)
{
 ...
}

Exception123 is key, only blocks catching that specific type should be identified. But the exception caught might not always be called e or exist at all. And the precise structure and formatting of the code-block isn't always the same... we might have try {.

Is it feasible to use regex in a Visual-Studio find-replace for this? I couldn't figure out how I would group/identify the main try block.

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Do you have nested `try/catch` occurences? Also, this might sound familiar: what have you tried? – HamZa Apr 25 '14 at 10:41
  • I don't - `MY_MACRO` is actually introducing them. – Mr. Boy Apr 25 '14 at 10:41
  • Not sure if [this](http://regex101.com/r/gM5iC3) is supported in visual studio: `(?is)try\s*\{.*?\}(?=\s*catch\s*\(\s*exception123[^)]*\))`, but that's the basic idea... – HamZa Apr 25 '14 at 10:47

1 Answers1

1

Ok. Sorry, I havn't saw the second part of your question :

I think there is the regex you want for search :

(try[\r\n]+{[^\}]+[\r\n]+)(})([\r\n]+catch\(Exception123)

And in replacement field :

MY_MACRO($1$2)$3
Polysymbol
  • 153
  • 4