C#程序使用正则表达式根据特定字段的值打印行?

时间:2013-03-07 06:17:45

标签: c# regex

我希望我的程序放弃appGUID的值为wxnull的所有行。如何使用正则表达式实现此目的?

我无法弄清楚它的正则表达式模式。请帮忙。

我的日志文件格式为:

INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419422 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO  [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419442 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf
INFO  [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx
INFO  [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx
INFO  [com.adobe.watson.vo.BugServices] WX New Bug: 3494441 Server: yukon.corp.adobe.com User:bey81694 appGUID: wx

我的代码在这里:

StreamReader reader = new StreamReader(@"C:\Users\karansha\Desktop\Karan Logs\20110717.txt");
string x = reader.ReadToEnd();

List<string> users = new List<string>();

Regex regex = new Regex(@"appGUID:\s*(?<value>.*?)\s");
MatchCollection matches = regex.Matches(x);

foreach (Match match in matches)
{
        var user = match.Groups["value"].Value;
        if (!users.Contains(user)) users.Add(user);
}

3 个答案:

答案 0 :(得分:1)

使用没有正则表达式的LINQ解析所有用户:

var users = File.ReadAllLines("20110717.txt")
                .Select(line =>
                {
                    string guidPrefix = "appGUID:";
                    int index = line.IndexOf(guidPrefix);
                    return line.Substring(index + guidPrefix.Length + 1);
                })
                .Where(user => user != "null" && user != "wx")
                .ToList();

如果日志格式不一致(通常你不应该依赖相同的格式,因为它应该是灵活的),那么正则表达式会更合适。它将处理在线更改appGUID位置或更改文本大小写:

Regex regex = new Regex(@"appGUID:\s*(?<user>\S+)", RegexOptions.IgnoreCase);
var users = File.ReadAllLines("data.txt")
                .Select(line => regex.Match(line))
                .Where(match => match.Success)
                .Select(match => match.Groups["user"].Value)
                .Where(user => user != "null" && user != "wx")
                .ToList();

UPDATE:实际上我会将解析部分移到单独的方法中,因为这是最有可能改变的事情。所以,你有类似的东西:

public List<string> GetUsersFrom(string fileName)
{
   return File.ReadAllLines(fileName)
              .Select(ParseUser)
              .Where(u => u != null && u != "null")
              .ToList();
}

private string ParseUser(string s) // Any implementation here
{
    var match = Regex.Match(s, @"appGUID:\s*(?<user>\S+)");
    if (!match.Success)
        return null;

    return match.Groups["user"].Value;
}

用法:

var users = GetUsersFrom("20110717.txt").Where(u => u != "wx");

答案 1 :(得分:0)

使用此

regularexpression.replace(@"(.*)?(wx|null)\b",.....)

并将其替换为string.empty,它会丢弃所有那些你不想要的行。

StreamReader reader = new StreamReader(@"C:\Users\karansha\Desktop\Karan Logs\20110717.txt");
string x = reader.ReadToEnd();

List<string> users = new List<string>();

Regex regex = new Regex(@"(.*)?(wx|null)\b");
var newString = regex.Replace(x, String.Empty);

快乐编码

答案 2 :(得分:0)

试试这个正则表达式:

appGUID:\s*(?<value>wx|null)\s