繁体   English   中英

C#的字符串列表>按正则表达式排序?

[英]c# list of strings > sort by regex ?

我对C#还是很陌生,但无法解决此问题(很可能是一个简单的问题)。

我有2个包含错误日志字符串的列表。 (让我知道使用字符串数组是否更好)

/* Example of list from host 1
2017-06-29 02:25:54.309 BST,ERROR,.......
2017-06-29 02:25:54.357 BST,ERROR,.......
2017-06-29 02:25:54.495 BST,ERROR,.......
2017-06-29 02:30:57.183 BST,ERROR,.......
2017-06-29 03:07:12.078 BST,ERROR,.......
2017-06-29 05:07:13.256 BST,ERROR,.......
2017-06-29 05:14:14.717 BST,ERROR,.......
2017-06-29 05:16:23.954 BST,ERROR,.......
2017-06-29 08:12:16.418 BST,ERROR,.......
2017-06-29 08:37:23.574 BST,ERROR,.......
2017-06-29 09:07:11.569 BST,ERROR,....... */
List<string> filteredLogFileC1 = filterLog(hostNameC1); //filterLog returns a List<string>

/* Example of list from host 2
2017-06-29 00:43:43.781 BST,ERROR,.......
2017-06-29 00:43:44.446 BST,ERROR,.......
2017-06-29 00:43:44.885 BST,ERROR,.......
2017-06-29 00:43:45.378 BST,ERROR,.......
2017-06-29 00:43:45.940 BST,ERROR,.......
2017-06-29 00:43:46.584 BST,ERROR,.......
2017-06-29 00:43:47.141 BST,ERROR,....... */ 
List<string> filteredLogFileC2 = filterLog(hostNameC2); //filterLog returns a List<string>

// Combine the 2 lists into one (the below practice might not be the best one but its working and I am happy at the moment :) )

/*
... Combined list
2017-06-29 08:12:16.418 BST,ERROR,.......
2017-06-29 08:37:23.574 BST,ERROR,.......
2017-06-29 09:07:11.569 BST,ERROR,....... 
2017-06-29 00:43:43.781 BST,ERROR,.......
2017-06-29 00:43:44.446 BST,ERROR,.......
2017-06-29 00:43:44.885 BST,ERROR,.......
...
*/
foreach (string line in filteredLogFileC2) filteredLogFileC1.Add(line);


// I need to sort the filteredLogFileC1 list by date.
// Below I have a regex that I've put together but I don't know how I can use it 

Regex sortReg = new Regex(@"(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}.\d{3})");

Issue: filteredLogFileC1.OrderBy( ???sortReg??? )

谢谢你的建议。

Sort方法适用于您的情况,但是由于基于文档它并不稳定(在日期相似的情况下不保留原始顺序),我建议使用OrderBy(稳定):

filteredLogFileC1 = filteredLogFileC1.OrderBy(dt => dt).ToList();

在上面的lambda (dt => dt) ,您说的是:按字符串自己的值对它们进行排序。

如果不是字符串,而是具有Date字段的数据结构,则可以说( dt => dt.Date )以便对该字段进行排序(只是清除可能会使您感到困惑的lambda)一点)。

我之前尝试过,但没有成功:

filteredLogFileC1.OrderBy(x => x)); // maybe I should have stored this into a new list ?
File.WriteAllLines(localPath + "combined.log", filteredLogFileC1);

这样,它为我工作,并生成输出:

File.WriteAllLines(localPath + "combined.log", filteredLogFileC1.OrderBy(x => x));

如果我正确理解您的任务-可以是:

filteredLogFileC1.Union(filteredLogFileC2).OrderBy(l => sortReg.Match(l).Value)

代码结果是IEnumerable。 您可以使用扩展方法.ToList()进行转换。 另外,如果Regex不匹配-结果值将为空字符串,否则将为所需的子字符串。

您应该从字符串创建日期,然后按日期排序

var matchedLines = filteredLogFileC1.Where(x => sortReg.IsMatch(x)).OrderBy(x => DateTime.ParseExact(sortReg.Match(x).Value, "yyyy-MM-dd HH:mm:ss.fff", null)); // lines that match date pattern, ordered by date value
var unMatchedLines = filteredLogFileC1.Where(x => !sortReg.IsMatch(x)); // Lines that do not match date pattern. Can be added at the top or bottom

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM