Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions to find C# class and method names

Tags:

c#

regex

parsing

I'm writing an impact analysis tool that will parse a bunch of selected file types and find the class and method calls inside the file (in this case a .cs file). I have managed to write a regular expression that will find if a line of code is a method call.

Regex re = new Regex(
               @"\b(public|private|internal|protected)\s*" +
                "(static|virtual|abstract)?\s*[a-zA-Z]*\s[a-zA-Z]+\s*" +
                "\((([a-zA-Z\[\]\<\>]*\s*[a-zA-Z]*\s*)[,]?\s*)+\)");

However; I can't figure out how to get the Method name out of the regular expression. It recognises a line as a Match but how do i pull out the actual method name. Any help on this would be amazing.

Furthermore; I'm not sure if this is how it's actually done but is there any other (up to date) c# file parsers that will be able to provide me a list of method names and class names from the file?

like image 803
Joshy Avatar asked Feb 23 '23 20:02

Joshy


1 Answers

You can put the part that represents a method into a group like this:

(?<method>[a-zA-Z]+)

Then you can access it like this:

Match match = regex.Match(line);
if (match.Success)
{
    string method = match.Groups["method"].Value;
}

However, your regex at the moment has various problems in terms of matching method names:

  • It doesn't handle generics (generic methods, parameters or return values)
  • It doesn't handle identifiers with digits or _ in
  • It doesn't handle methods with the default access modifier
  • It will match string literals containing something looking like a method declaration

To get method names out of a C# file, you really need something which understands C#. Note that the Roslyn project may well make this sort of thing a lot easier - we'll see when the first preview comes out, very soon now...

like image 92
Jon Skeet Avatar answered Mar 01 '23 23:03

Jon Skeet