[英]Regex to match string and all content after it to another occurrence of the same string
我正在嘗試將每個新的錯誤日志行與C#中的Regex匹配。 我希望每次出現的日期都有新的匹配項
[yyyy-MM-dd HH:mm:ss,ffff]
這是示例數據和我當前(不起作用)的解決方案:
(\[[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}\:[0-9]{2}\:[0-9]{2}\,[0-9]{3}\])(.*)
[2018-06-28 00:58:14,596] - INFO - [54] - ProcessItemController - Processing url: http://somehttp.com/something.xml/
[2018-06-28 00:58:14,612] - ERROR - [54] - ProcessItemController - Processing Failed
System.UnauthorizedAccessException: Access to the path 'D:\SomePath\something.xlsx' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at Something.Processors.PathAttachmentExtractorProcessor.XmlParser(String path, String outputPath, ProcessingItem processingItem)
at Something.Processors.EurekaInfoPathAttachmentExtractorProcessor.ProcessItem(ProcessingItem processingItem)
at Something.ProcessItemController.Process(Item item)
[2018-06-28 00:58:14,627] - INFO - [69] - ProcessItemController - Processing url: http://someurl.com/cables.xml/
[2018-06-28 00:58:14,627] - ERROR - [69] - ProcessItemController - Processing Failed
System.UnauthorizedAccessException: Access to the path 'D:\SomePath\anotherSomething.xlsx' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at Something.Processors.PathAttachmentExtractorProcessor.XmlParser(String path, String outputPath, ProcessingItem processingItem)
at Something.Processors.PathAttachmentExtractorProcessor.ProcessItem(ProcessingItem processingItem)
at Something.ProcessItemController.Process(Item item)
https://regex101.com/r/6BJpKF/1/
問題是,當有錯誤日志時,該模式不會獲得新行中的異常描述。
有沒有一種方法可以在單獨的匹配中獲取日期的每次出現之間的所有數據(帶有日期本身)?
嘗試以下解決方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
string input = File.ReadAllText(FILENAME);
string pattern = @"^(?'date'\[[^\]]+)\]\s+-\s+(?'type'[^\s]+)\s+-\s+\[(?'message'[^\[]*)";
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
foreach (Match match in matches)
{
Console.WriteLine("Date : '{0}', Type : '{1}', Error Number = '{2}', Message = '[{3}'",
match.Groups["date"], match.Groups["type"], match.Groups["errNum"], match.Groups["message"]);
}
Console.ReadLine();
}
}
}
僅使用正則表達式應該可以工作:
string datetimeRegex = @"\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}\]";
var rx = new Regex(@"(?:^|(?<=\n))" + datetimeRegex + @"(?:(?!(?<=\n)" + datetimeRegex + @").)*", RegexOptions.Singleline);
Match m;
int ix = 0;
while ((m = rx.Match(str, ix)).Success)
{
// Your log
string log = m.Value;
ix += log.Length;
}
但是我不是很高興。 我認為這是可行的。 請注意,每個log
將保留其最終\\r?\\n
。 (?:^|(?<=\\n))
意思是“字符串的開頭或換行”。 (?!(?<=\\n)" + datetimeRegex + @")
表示以\\n
開頭的日期時間將停止.*
匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.