Reputation: 523
Hi I am reading a log file(Text File) using C# in my application and I am reading the log file using dates . And also I created a Regex format for get the exception by date basis.
Now I am getting the exception by using this regex format successfully but I am not able to get the Exception line properly. The last letter of the exception line is missing for me when i am going to use the below regex format. Tell me What I did wrong in this line. Can anyone please tell me the solution to get the full line of the exception.
Thanks in advance.
My Code is:
var matches = Regex.Matches(sb.ToString(), @"(?<date>\d{4}\-\d{2}\-\d{2})\s(?<message>(.(?!\d{4}\-\d{2}\-\d{2}))+)");
My txt file looks like
2012-06-21 04:35:30,177|| [5]|| DEBUG|| GID.AAFramework.Shared.LdapAuthentication.PrincipleImpl|| PrincipleImpl: IsInRole(string role)- Entered
2012-06-21 04:35:30,177|| [5]|| DEBUG|| GID.AAFramework.Shared.LdapAuthentication.PrincipleImpl|| PrincipleImpl: IsInRole(string role)- IsInRole(ADMINUSER)
results : True
in my log file all the lines starts with date but in some lines messages are too long so it comes to next line..so i want to read the file starts with date means i misses the line starts with that message...
Upvotes: 1
Views: 1688
Reputation: 92986
To answer your first question, to get the last missing letter, just change the position of the dot in your pattern:
var matches = Regex.Matches(sb.ToString(), @"(?<date>\d{4}\-\d{2}\-\d{2})\s(?<message>((?!\d{4}\-\d{2}\-\d{2}).)+)");
This is now matching the next letter if it is not the start of the lookahead pattern. Your pattern matched the next character if the lookahead pattern is not following that character.
I am not sure what your question is in the last part you added about the next line ...
Upvotes: 1
Reputation: 43673
Try to use:
@"(?<date>\d{4}\-\d{2}\-\d{2})\s+(?<message>(.(?!\d{4}\-\d{2}\-\d{2}))+.)"
Upvotes: 1