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

时间:2017-06-29 11:14:23

标签: c# regex string list sql-order-by

我对c#很新,但我无法解决这个问题(很可能是一个简单的问题)。

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

/* 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??? )

感谢您的建议。

4 个答案:

答案 0 :(得分:0)

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

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

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

如果那不是字符串,而是一个具有Date字段的数据结构,您可以说(dt => dt.Date)以便按该字段排序(只是为了清除lambda)这似乎让你有点困惑。)

答案 1 :(得分:0)

我以前试过这个并且没有用:

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

答案 2 :(得分:0)

如果我理解你的任务 - 这可以是:

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

代码结果是IEnumerable。您可以使用扩展方法.ToList()来转换它。此外,如果Regex无法匹配 - 结果值将是一个空字符串,否则它将是所需的子字符串。

答案 3 :(得分:0)

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

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