Use Visual Studio Regular Expression Search to Search for Empty Try / Catch Blocks

I found the appropriate version of this question in C #, but I was not able to convert it to search for VB.net syntax, and regular expressions are not my strong suit, so I will post what I received, and then threw myself at the mercy of the stack.

I managed to collect the regex:

Catch+\s+\b(_\w+|[\w-[0-9_]]\w*)\b+\s+As+\s+Exception+[\S|\r?\n]

which matches Catchesunspecified exceptions (plus a line, because this is part of what I'm working on). I want to expand this to look for empty Try / Catches, but apparently I missed something.

I am adding *+(Catch|End|Finally)to the previous regex to give:

Catch+\s+\b(_\w+|[\w-[0-9_]]\w*)\b+\s+As+\s+Exception+[\S|\r?\n]*+(Catch|End|Finally)

which, as far as I can tell, should match any mounting of spaces / lines and then the actual end-of-catch keyword, but I stop getting results at that point.

What am I bringing here?

+4
source share
1 answer

Try the following:

(Catch(\s?.+As.+)?(\r?\n|\s)*)+(Finally)?(\r?\n|\s)*End Try

It's time to work. Sometimes a visual studio is a real pain: D Secret is the correct matching of line breaks. I repeat (\r?\n|\s)*quite a lot. This matches \r\neither \neither \szero or many times.

What did you screw up?

. VB.net - . (. *) , ( , , , !). .

+4

All Articles