Search code examples
c#regexregex-negationqregexp

How to match methods without a specific category


I need to remove some tests from several classes without some specific category

how do I match than?

in this example i want to remove only the methods 'Bar' and 'BarTwo'

namespace MyNameSpace
{
    /// <summary>
    /// Some Stuff
    /// </summary>
    public class MyClass
    {
        [CategoryA]
        public void Foo()
        {
        }

        public void Bar()
        {
        }

        [CategoryA]
        public void Baz()
        {
        }

        public void BarTwo()
        {
        }
    }
}

Solution

  • You can use the following to match:

    (?<!]\s)public\s+void[^[]+(?=\[|\s*}\s*}$)
    

    And replace with '' (empty string)

    See DEMO

    Note1 : I assumed all your methods start with public if not you can use the following:

    (?<!]\s*)(public|private|protected)\s+void[^[]+(?=\[|\s*}\s*}$)
    

    Note2 : I also assumed that there wont be any [ or ] in your methods.