Poul K. Sørensen
Poul K. Sørensen

Reputation: 17560

Can i group a text file with Regex based on line patterns

Given a file:

Timestamp: some text and a number 1
Timestamp: some text and a number 33
Timestamp: some text and a number 1
Timestamp: some text and a number 22
Something totally different, maybe a new pattern
Timestamp: some text and a number 4
Timestamp: some text and a number 2
Something totally different, maybe a new pattern
Something totally different, maybe a new pattern

I would like to get groupings of line 1 to 4(TYPE1) and line 5(TYPE2), line 6,7(TYPE1) and line 8,9(TYPE2).

Can this be done in one regualar expression or should i create one expression for each type and then check line by line and if the previous line is the same type?

At the end i need to return a list of groupings with pair(int start_char, int end_char)

Upvotes: 1

Views: 102

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13599

you can try this

string[] lines = System.IO.File.ReadAllLines("your taext file");

       var Groups =( 
                from w in lines 
                group w by w[0] into g 
                select new { FirstLetterLine = g.Key, Lins = g }); 

Upvotes: 1

Related Questions