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
我是 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