正则表达式提取包含异常堆栈跟踪的多行

时间:2017-07-11 13:26:27

标签: regex regex-greedy regex-group

从日志文件中使用异常strack跟踪提取多行错误的正确正则表达式是什么。这是我的例子。

Verbose;MyComputer;07.02.2017 12:42:48,831;Area=;SubArea=;SessionId=;StepId;User=;Message=Repository CareProviderRepository:START GetCareProviderByZsrMethod with ZSR: H110702
Error;MyComputer;07.02.2017 12:42:51,409;Area=;SubArea=;SessionId=;StepId;User=;Message=Repository CareProviderRepository:Fail to get CareProviderMethod with Zsrnumber: H110702
Error;MyComputer;07.02.2017 12:42:51,933;Area=;SubArea=;SessionId=;StepId;User=;Message=Services:Exception: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Name_DE'.
Invalid column name 'Name_FR'.
Invalid column name 'Name_IT'.
Invalid column name 'DefaultText'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
Verbose;MyComputer;07.02.2017 12:42:52,361;Area=;SubArea=;SessionId=;StepId;User=;Message=Business NetworkManager:START Get: Network with Code: 95; withNetworkMembers: False
Verbose;MyComputer;07.02.2017 12:42:52,369;Area=;SubArea=;SessionId=;StepId;User=;Message=Repository NetworkRepository:START Get: 95
Error;MyComputer;07.02.2017 12:42:51,933;Area=;SubArea=;SessionId=;StepId;User=;Message=Services:Exception: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Name_DE'.
Invalid column name 'Name_FR'.
Invalid column name 'Name_IT'.
Invalid column name 'DefaultText'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
Verbose;MyComputer;07.02.2017 12:42:52,361;Area=;SubArea=;SessionId=;StepId;User=;Message=Business NetworkManager:START Get: Network with Code: 95; withNetworkMembers: False
Verbose;MyComputer;07.02.2017 12:42:52,369;Area=;SubArea=;SessionId=;StepId;User=;Message=Repository NetworkRepository:START Get: 95

以下是我的RegEx:(Error;(?:.*\r?\n?)\s)

这个正则表达式只选择我一行或错误。其实我想要一个正则表达式可以选择我3错误(第一个错误是单行,后来2错误是多行)。

我的正则表达式不是在错误行末尾转义CRLF或换行符。

每条记录的尾随边界为Debug;Verbose;Info;Error;

有任何建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用以下正则表达式

/^Error;.*(?:\r?\n(?!Error;|Verbose;|Info;|Debug;).*)*/gm

请根据您使用的语言调整符号。

<强>详情

  • ^ - start of a line ( m modifier makes the ^`匹配行的开头而不是整个字符串)
  • Error; - 匹配文字子字符串Error;
  • .* - 与该行的其余部分匹配
  • (?:\r?\n(?!Error;|Verbose;|Info;|Debug;).*)* - 0+序列:
    • \r?\n(?!Error;|Verbose;|Info;|Debug;) - 括号内没有任何替代品的换行符
    • .* - 整条线。