locked
need a regex pattern that matches getters and setters RRS feed

  • Question

  • I need a regex pattern that will match get { ... } and set { ... }. I am not concerned about automatic properties. I'm not sure how to set up the pattern so that nesting is taken into account. I need the pattern to match the entire getter and/or setter.
    Saturday, May 4, 2013 2:15 PM

Answers

  • See if the code below works.  I just made up a crazy sample to make sure the nesting worked properly.  I use the webpage below as reference

    http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.90).aspx

    Because you are uisng curly brakets there must be a backslash before these characters.  To make sure I go the last occurance I used [^\}]*$ which indicates by end of the expression should not contain any curly close brackets.

            static void Main(string[] args)
            {
                string sample = "get {ab  { cd } ef } gh";
                Regex expr = new Regex(@"([gs]et\s*\{.*\})[^\}]*$");
                MatchCollection m = expr.Matches(sample);
                string result = m[0].Groups[1].ToString();
            }


    jdweng

    • Marked as answer by Mike Feng Thursday, May 9, 2013 4:52 AM
    Saturday, May 4, 2013 5:52 PM
  • According to an article -- http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396452.aspx -- try this:

    get\s*{[^{}]*(((?<Open>{)[^{}]*)+((?<Close-Open>})[^{}]*)+)*(?(Open)(?!))}

    To use it in Search panel of Visual Studio 2012, try this:

    get\s*{([^{}]|\n)*(((?<Open>{)([^{}]|\n)*)+((?<Close-Open>})([^{}]|\n)*)+)*(?(Open)(?!))}

    However it does not consider comments and strings.


    • Edited by Viorel_MVP Saturday, May 4, 2013 7:23 PM
    • Marked as answer by Mike Feng Thursday, May 9, 2013 4:52 AM
    Saturday, May 4, 2013 7:20 PM

All replies

  • See if the code below works.  I just made up a crazy sample to make sure the nesting worked properly.  I use the webpage below as reference

    http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.90).aspx

    Because you are uisng curly brakets there must be a backslash before these characters.  To make sure I go the last occurance I used [^\}]*$ which indicates by end of the expression should not contain any curly close brackets.

            static void Main(string[] args)
            {
                string sample = "get {ab  { cd } ef } gh";
                Regex expr = new Regex(@"([gs]et\s*\{.*\})[^\}]*$");
                MatchCollection m = expr.Matches(sample);
                string result = m[0].Groups[1].ToString();
            }


    jdweng

    • Marked as answer by Mike Feng Thursday, May 9, 2013 4:52 AM
    Saturday, May 4, 2013 5:52 PM
  • According to an article -- http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396452.aspx -- try this:

    get\s*{[^{}]*(((?<Open>{)[^{}]*)+((?<Close-Open>})[^{}]*)+)*(?(Open)(?!))}

    To use it in Search panel of Visual Studio 2012, try this:

    get\s*{([^{}]|\n)*(((?<Open>{)([^{}]|\n)*)+((?<Close-Open>})([^{}]|\n)*)+)*(?(Open)(?!))}

    However it does not consider comments and strings.


    • Edited by Viorel_MVP Saturday, May 4, 2013 7:23 PM
    • Marked as answer by Mike Feng Thursday, May 9, 2013 4:52 AM
    Saturday, May 4, 2013 7:20 PM