c# regex - select class properties names, methods name and fields from class file (.cs)

Viewed 2448

I want to match (select from class file) methodsname, properties name and fields name.

This is example class:

class Perl
{
    string _name;
    public string Name { get; set; }
    public Perl()
    {
    // Assign this._name
    this._name = "Perl";
    // Assign _name
    _name = "Sam";

    // The two forms reference the same field.
    Console.WriteLine(this._name);
    Console.WriteLine(_name);
    }
    public static string doSomething(string test)
    {
        bla test;
    }
}

I got code for the methods:

(?:public|private|protected)([\s\w]*)\s+(\w+)\s*\(\s*(?:\w+\s+(\w+)\s*,?\s*)+\)

And i got questions:

  • this above regex code gets all methods and it works pretty well but also i want it to select method name but without parameters and accessors. So from exaplmce class using my code result will be: public Perl() and public static doSomething(string test) but i want that kind of result: Perl() and doSomething(). So - my code matches good but i want result to be displayed just like I wrote in previous sentence.
  • how to select properties ? with result displayed: type and property name. So from exaple class result will be: string Name
  • how to select fields with result: type field_name. In out case it will be: string _name
4 Answers
Related