Parsing a text file in fields using several types of delimiters

I am trying to parse log files from chat using C #, the problem I am facing is that it is not meant to be parsed since it does not use standard delimiters. Here is an example of a typical line from a file:

 2010-08-09 02:07:54 [Message] Skylar Morris -> (ATL)City Waterfront: I'll be right back 
 date time messageType userName -> roomName: message

The fields I would like to save are: Date and time related as DateTime type

MessageType

Username

roomName

message

If it was separated by a standard separator such as space, tab or comma, that would be pretty simple, but I don't understand how to attack this.


As a continuation, using this code as a template:

List<String> fileContents = new List<String>();
string input = @"2010-08-09 02:07:54 [Message] Skylar Morris -> (ATL)City Waterfront: I'll be right back";
string pattern = @"(.*)\[(.*)\](.*)->(.+?):(.*)";

foreach (string result in Regex.Split(input, pattern))
{
   fileContents.Add(result.Trim());
}

I get 7 items (one empty before and after) 5 that are expected. How can i fix this?

foreach (string result in Regex.Split(input, pattern)
        **.Where(result => !string.IsNullOrEmpty(result))**)
{
   fileContents.Add(result.Trim());
}

, .

+3
2

, " , , " , ". ."?

, .

:

([\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}) \[([\w]+)\] ([a-zA-Z0-9 ]+) -> (\([\w]+\)[a-zA-Z0-9 ]+): (.*)

. , , .

+5

:

.*\[(.*)\](.*)->(.+?):(.*)

, [] [] → → : - .:)

+2

Source: https://habr.com/ru/post/1792703/


All Articles