0

我希望我的程序丢弃所有值为appGUIDorwx的行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: 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);
}
4

3 回答 3

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();

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

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");
于 2013-03-07T06:36:11.023 回答
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);

快乐编码

于 2013-03-07T06:28:54.530 回答
0

试试这个正则表达式:

appGUID:\s*(?<value>wx|null)\s
于 2013-03-07T06:30:07.517 回答