Regex to check for “+=” inside of a method

2019-07-17 15:20发布

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 +=.

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()

I tried: DetachEvents()[.]+=[.] thinking the period would find anything before and after the += but it doesn't find anything.

1条回答
Root(大扎)
2楼-- · 2019-07-17 15:37

Try escaping the +:

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()[^}]*\+=
查看更多
登录 后发表回答