slimjourney
slimjourney

Reputation: 103

Regex - Include all other line sections in their own capture groups

I have a log file with these contents:

Log Started
Created Date: YY/MM/DD Time: HH:MM:SS Start
Added resources at module on YY/MM/DD HH:MM
Module 2 excecute
Resource depleted at HH:MM n pieces
Open YY/MM/DD HH:MM:SS Log to refer
Target end date of new resource YY/MM/DD approved
Log Ended. Result OK

Legend:

Notes:

I currently have the regex below, but it can only capture the date and time stamps on each line:

(\d{2}(\d{2})?\/\d{2}\/\d{2}(\d{2})?)|(\d{2}:\d{2}(:\d{2})?)

But, I need to be able to capture the whole line, and place each important and unimportant sections of data in a group.

Upvotes: 1

Views: 66

Answers (2)

Senthil
Senthil

Reputation: 55

First you have to group each line using (.*)\n. Then you have to replace the date format using the regex from the grouped string.

Check here. https://msdn.microsoft.com/en-us/library/e7f5w83z(v=vs.110).aspx

Upvotes: 1

Ethan
Ethan

Reputation: 4375

I'm not sure how one would do this in C# (as I've never used it before), but here are the regexes I would use:

1. Get the whole line

(.*)\n

Basically just matches everything until it finds a new line char \n.

2. Get the non-timestamps on every line

Duplicate the log file into a new temporary variable and remove all the timestamps and split the resulting string by the newline chars \n DEMO

3. Get the unimportant timestamps

(\d{2}(\d{2})?\/\d{2}\/\d{2}(\d{2})?)|(\d{2}:\d{2}(:\d{2})?)

Your regex was quite good and worked like a charm :)

Upvotes: 1

Related Questions