4

I have like 30 C# class files that all have the a method with the same name but not the same code inside, I want to change this by searching the C# file for a regex match of the method and whatever is inside. So far my regex can find the first line of the method (thats the easy part) but I cannot figure out how to find the opening curly brace and the closing curly brace with uknown number of characters in between.

Here is my attempt but I'm no expert

private void btnDelete_Click\(object sender, EventArgs e\)
\{
\S
\}

And this is the method I need to find

    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (gridView1.RowCount <= 0) return;

        this.rowHandle = gridView1.FocusedRowHandle;
        currentState = state.delete;

        MessageResult res = new Global.sysMessages.sysMessagesClass().viewMessage(
            MessageType.Warning,
            "Delete Warning",
            "You are about to delete this record from the system forever.\nare you sure you want to continue?",
            ButtonTypes.YesNo,
            MessageDisplayType.Small);

        if (res == MessageResult.Yes)
        {
            delete(rowHandle);
            loadGrid();
        }
        currentState = state.idle;
    }

Any help is welcome, Thanks

10
  • 1
    This is where Roslyn could help. But in any case, why do you want to do this? What are you trying to achieve by doing this? What about reflection, will this help? Commented Nov 27, 2013 at 8:55
  • You probably shouldn't use Regex to parse code, about 80% of the time it will match things that you didn't intend to. Commented Nov 27, 2013 at 8:56
  • Does this need to cater for anonymous methods as well, in which case this is going to get complicated Commented Nov 27, 2013 at 9:11
  • 1
    I would say that it is harder and more error prone to automate this than to just do it manually file by file... Commented Nov 27, 2013 at 9:15
  • 1
    If you have 30 methods with the same name in different files and almost the same implementation, shouldn't you instead use some SOLID design principles? And start changing this to a class exposing these methods and that uses sub methods for the differences? This way you only have to change one method that will work for all. Commented Nov 27, 2013 at 10:08

3 Answers 3

1

Solution

private\s+void\s+btnDelete_Click\s*\(object\s+sender,\s*EventArgs\s+e\)\s*\{.+;\s+\}\s*

Regular expression visualization

Demo

Here

Discussion

The central problem is how to match the final curly brace. I assume that the btnDelete_Click method is not placed at the end of the class. Otherwise, this regex is useless.

This regex can be used for quick and dirty work that has a one-time use life span.

Sign up to request clarification or add additional context in comments.

3 Comments

In this case it finds the whole method up to the point where there is a line of code just before the methods ending "}"... I have a line that says "state = states.loading;" it doesn't find that line
@5tar-Kaster Can you add the method body that contain this line please ?
I think I'm going to update the question to add the whole delete code, but I'm beginning to think that this may be a tad more difficult than I had first thought.
1

Try this :)

\s*private\svoid\sbtnDelete_Click\(.*?\)\s*\{.+?\}(?!['"].*\}.*['"].*)

Don't forgot the DOTALL, ie . matches everything including \n.

Everything between \{.+?\} is the method's code.

UPDATE:

As pointed out, the above regex will fail, so here is a recursive that seems to do the trick:

{(?:[^{}]+|(?R))*}

Also, it will fail if there are } inside string vars, something I'm looking into as using parsers for simple jobs can be overkill sometimes :)

1 Comment

this doesn't find the whole method unfortunaly, if you have an if statement within the method it will stop looking beyond the "}"of the if...
-1

If the method isn't at the end of the file and you know the scope of the following method (in this example "private"), you could just use

void btnDelete_Click(.|\n)*(?=private)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.