Prince Ashitaka
Prince Ashitaka

Reputation: 8773

How to get a method name which has specific attribute using regex

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.

Upvotes: 0

Views: 145

Answers (1)

jjchiw
jjchiw

Reputation: 4445

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....

Upvotes: 1

Related Questions