2017-06-29 50 views
0

我對c#很新,但我無法解決這個問題(最可能是一個簡單的問題)。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???) 

謝謝您的建議。

+0

有'.AddRange'方法將第二列表添加到第一個。 –

+1

「按正則表達式排序」是什麼意思?'最終結果應該是什麼? –

+0

如果你的日期總是相同的格式(並且總是從年份到毫秒),你應該可以使用標準的字母排序。 –

回答

0

Sort方法將工作你的情況,但因爲它的基礎上的文檔並不穩定(不保留在類似日期的情況下,原來的順序),我建議使用排序依據(這是穩定的):

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

在上面的lambda (dt => dt)中,你的意思是:按照自己的值排序字符串。

如果這不是一個字符串,而是一個數據結構,例如有一個Date字段,您可以說(dt => dt.Date)爲了按該字段進行排序(只是爲了清除lambda,這似乎讓您感到困惑一點位)。

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

'OrderBy'返回一個新的'IEnumerable',並不會更新您正在操作的那個。 – user3185569

0

如果我明白你的任務權 - 這可以是:

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

代碼結果是IEnumerable的。您可以使用擴展方法.ToList()來轉換它。此外,如果正則表達式不匹配 - 結果值將是一個空字符串,否則它將是所需的子字符串。

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 
+0

如果不匹配,您將得到一個例外 – Kantora

+0

我編輯了答案,因此當行與日期模式不匹配時,不會出現異常。 –