0

Help needed in parsing method name which has specific attribute.

Rules:
1) All the methods attributed with minimum '[Test' should be listed.
2) methodName will have a space character before the name and '(' symbol at the end of the name. Most likely next line to the Test attribute or on the second line.

Sample 1:

[Test]
public Type methodName(parametes ...)

Sample 2:

//[Test]
public Type methodName(parametes ...)

Sample 3:

[Test (, some names etc)]
public Type methodName(parametes ...)

Sample 4:

[Test (, some names etc)]
[Other optional attributes]
public Type methodName(parametes ...)

Expected Result: methodName

I tried couple of suggestions like this Regex Match all characters between two strings. But, not successful.

3
  • Why do you want to use Regex? Commented Apr 16, 2013 at 12:06
  • You should be using Reflection to have the work done for you. Commented Apr 16, 2013 at 12:08
  • I was given list of files, each file has classes with names different from the file name. I felt regex would be more opt n less manual in this scenario. Commented Apr 16, 2013 at 12:21

1 Answer 1

1

Not so fancy with all the regex stuff, but it works, with the sample cases....

var lines = File.ReadAllLines(@"c:\temp\samples.txt");

var matched = false;
foreach (var line in lines)
{
    if(matched)
    {
        var match = Regex.Match(line, @"public");
        if(match.Length > 0)
        {
            matched = false;
            match = Regex.Match(line, @"[a-zA-Z_]+( )?(?=\()"); 
            Console.WriteLine (match.Value);
        }
    }
    else
    {
        matched = Regex.IsMatch(line, @"\[.*Test.*\]");
    }
}

Or you could run all the files with the unit runner and it will list all the names of the tests....

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

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.