c#中使用正则表达式过滤值

本文关键字:过滤 正则表达式 | 更新日期: 2023-09-27 18:02:51

我希望我的程序丢弃所有"appGUID"的值为"wx"或"null"的行

我如何使用正则表达式实现这一点。在此之后,它应该返回"WX编辑Bug"的唯一值。我试过了,但看起来我的代码有bug。

我无法找出它的正则表达式模式。请帮助。

我的日志文件的格式是:

(com.adobe.watson.vo

信息。WX编辑Bug: 3494430用户:xinche appGUID: null INFO[com.adobe.watson.vo。WX编辑Bug: 3494430用户:xinche appGUID: null INFO[com.adobe.watson.vo。WX编辑Bug: 3419432服务器:用户:预发布appGUID: fcdd2153-bbdf INFO[com.adobe.watson.vo。WX编辑Bug: 3419432服务器:用户:预发布appGUID: fcdd2153-bbdf INFO[com.adobe.watson.vo。WX编辑Bug: 3494430用户名:xinche appGUID: wxinfo[com.adobe.watson.vo。WX编辑Bug: 3494430用户名:xinche appGUID: wxinfo[com.adobe.watson.vo。WX编辑Bug: 3494430用户:xinche appGUID: null INFO[com.adobe.watson.vo。WX编辑Bug: 3494430用户:xinche appGUID: null INFO[com.adobe.watson.vo。WX编辑Bug: 3419432服务器:用户:预发布appGUID: fcdd2153-bbdf INFO[com.adobe.watson.vo。WX编辑Bug: 3419432服务器:用户:预发布appGUID: fcdd2153-bbdf

我的代码在这里:

IEnumerable<string> textLines
                          = Directory.GetFiles(@"C:'Users'karansha'Desktop'Dummy'", "*.*")
                            .Select(filePath => File.ReadLines(filePath))
                            .SelectMany(line => line);
            string x = string.Join(",", textLines);
            Regex regex = new Regex(@"(.*)?(wx|null)'b");
            var newString = regex.Replace(x, String.Empty);
            string discard = string.Join(",", newString);
            List<string> users = new List<string>();
            Regex regex1 = new Regex(@"WX Edit Bug:'s*(?<value>.*?)'s+Server");
            MatchCollection matches1 = regex1.Matches(discard);
            foreach (Match match in matches1)
            {
                var Editbug = match.Groups["value"].Value;
                if (!users.Contains(Editbug)) users.Add(Editbug);
            }
            int WX_Edit = users.Count;

c#中使用正则表达式过滤值

使用您提供的代码,并假设查询的第一部分有效,您可以尝试使用Where子句(未经测试):

IEnumerable<string> textLines
    = Directory.GetFiles(@"C:'Users'karansha'Desktop'Dummy'", "*.*")
               .Select(filePath => File.ReadLines(filePath))
               .SelectMany(line => line)
               .Where(line => line.Contains("appGUID: null") || line.Contains("appGUID: wx"))
               .ToList();