开发者

Visual studio regex to match swallowed exceptions

I need a Visual Studio regex to match swallowed exceptions.

This is as far as I got, but it still matches valid code like this:

catch (ArgumentNullException)
{
  //...
  throw;
}


catch:Wh*\(:Wh*.*Exception.*\):Wh*\{[:Wh*.*]*[^(throw:Wh*.*;)].*

How can I fix this?

Edit: Am I to take it from开发者_高级运维 the comments that testing for the absence of a pattern in a regular expression in Visual Studio is not possible? - This is the thrust of my question. I would like to put aside questions of validity of approach (I am fully aware of FxCop et al, lexing and parsing, and this seminal post. I am also aware that swallowing exceptions is sometimes OK).


I'm going to answer my own question here. This is an academic question, because, or course, there is a fundamental difference in the capabilities of a context free grammar and a regular expression as previous commenters have so eagerly pointed out, plus there are manifold existing non-regex -based tools that will perform this functionality for me.

I wanted to explore the negative lookahead capabilities of Visual Studio. According to reports in the comments here and here, the negative lookahead behavior of Visual Studio does not behave as you might expect under some circumstances (to the point of some calling it buggy).

I am not an expert on regular expressions, but i would expect the regex (throw) to match a line containing only throw - and indeed it does. I would also expect ~(throw) (this is the syntax for the negative lookahead in Visual Studio) not to match that same line - but, in Visual Studio, it does.

Having toyed with refining my original regular expression, including simplifying the test pattern, I come to the conclusion that performing negative lookaheads using the current implementation of Visual Studio's regular expression search is difficult, nigh impossible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜