正则表达式检查“+="方法内部 [英] Regex to check for "+=" inside of a method
问题描述
通过我们的代码库,我们的许多自定义控件都有一个 DetachEvents
方法,该方法在控件被释放时被调用.很多时候,其他开发人员错误地是事件处理程序
Going through our codebase, many of our custom controls have a DetachEvents
method that gets called with the control is being disposed. Quite frequently, other developers incorrectly are the event handler
private void DetachEvents()
{
myControl.Click += new EventHandler(myControl_Click);
}
当然,什么时候应该是 -=
而不是 +=
.
When it should, of course, be -=
instead of +=
.
我正在尝试为 Find & 想出一个正则表达式.替换 Visual Studios 中的工具,只找到包含 +=
的 DetachEvent
方法,但我不知道如何做.如果我继续查找 DetachEvents()
I'm trying to come up with a regex for the Find & Replace tool in Visual Studios to only find me DetachEvent
methods that contain the +=
but I cannot figure out how to. I'll be here all day if I just keep Finding every occurance of DetachEvents()
我试过:DetachEvents()[.]+=[.]
认为该句点会在 +=
之前和之后找到任何东西,但它没有找到任何东西.
I tried: DetachEvents()[.]+=[.]
thinking the period would find anything before and after the +=
but it doesn't find anything.
推荐答案
尝试转义 +
:
DetachEvents().*\+=
但这将匹配 DetatchEvents()
之后的任何 +=
实例.您可能想要使用 [^}]
而不是 .
,假设您在 DetachEvents
方法中没有任何其他代码块:
But this will match any instance of +=
which follows DetatchEvents()
. You might want to use [^}]
instead of .
, assuming you don't have any other code blocks inside the DetachEvents
method:
DetachEvents()[^}]*\+=
这篇关于正则表达式检查“+="方法内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!