Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式搜索特定的try/catch块_C#_Regex_Visual Studio_Exception Handling - Fatal编程技术网

C# 使用正则表达式搜索特定的try/catch块

C# 使用正则表达式搜索特定的try/catch块,c#,regex,visual-studio,exception-handling,C#,Regex,Visual Studio,Exception Handling,正在考虑如何搜索其catch块中没有log.Error(ex)的try/catch块。所谓搜索,我指的是使用Visual Studio内置的正则表达式搜索 正则表达式将发现如下块: try { CallSomeExceptionalFunction(); } catch(Exception ex) { CallSomething(); // missing error handling } regex应跳过此块,因为它包含日志。错误: try { CallSom

正在考虑如何搜索其catch块中没有log.Error(ex)的try/catch块。所谓搜索,我指的是使用Visual Studio内置的正则表达式搜索

正则表达式将发现如下块:

try
{
    CallSomeExceptionalFunction();
}
catch(Exception ex)
{
    CallSomething();
    // missing error handling
}
regex应跳过此块,因为它包含日志。错误:

try
{
    CallSomeExceptionalFunction();
}
catch(Exception ex)
{
    log.Error(ex);
}

不能使用正则表达式解析基于上下文无关语法的编程语言


想象一下,在catch块中有一个try-catch块,在catch块中有另一个try-catch块,其中只有一个包含log.Error。

假设您想在代码中执行此操作,我不会担心正则表达式,我只会搜索单词
catch
,然后计算之后开始大括号的数量,
{
并从该计数中减去任何结束大括号
}
,当我发现一行包含
log.Error
时停止,如果在计数达到零之前找不到该行,然后您得到了一个缺少日志的实例。

您的意思是要使用Visual Studio内置的正则表达式搜索?或者你想在一个分析源代码的C#程序中实现它?另外,在
try
catch
块之后是否可以有多对大括号?如前所述,如果您有任意的catch块,正则表达式就不能真正帮助您。如果所有的catch块都与示例中的完全相同,那么就可以完成。但我敢打赌,只要搜索所有代码中的“catch”短语,然后手动检查是否使用首选的记录器,您就会花费更少的时间和精力。Tim Pietzcker:是的,我想使用Visual Studio内置的正则表达式searchAhmet Kakıcı:不,我不想懒惰,我不想提高效率。这就是VB语法的优势所在,匹配
End Try
/
End函数而不是任何旧的
}
使regexing语句和控制结构变得更容易但这需要编写自定义搜索工具吗?我不能在Visual Studio内置搜索中完成吗?@Ivan:我误解了,我以为这就是你所做的,因为你有
C
作为标记。然而,我认为您可以编写一个VS宏来实现这一点(或者如果您想获得高级,可以编写一个VS加载项),这样您仍然可以在VS中使用它。