I'm trying to analyse lots of powershell scripts in a number of directories and i want to pull any Catch code blocks into a list/ variable.
I'm trying to write a regex to select any block in the following formats
Catch
{
write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_"
}
Catch{
write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_" }
Catch {write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_"}
Essentially anywhere there is a catch followed by {}, ignoring any line breaks between the word "Catch" and the braces and after the braces, ignoring case.
I want the entire contents between the {} returned too so i can do some additional checks on it.
The best i have managed to come up with is:
\b(\w*Catch\w*)\b.*\w*{\w.*}
Which will match if its all on one line.
I'll be doing this in powershell so .net or powershell type regex would be appreciated.
Thanks.