我可以基于行模式用Regex对文本文件进行分组吗?

本文关键字:文件 文本 于行 模式 Regex 我可以 | 更新日期: 2023-09-27 18:06:44

给定一个文件:

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

我想得到第1到4行(TYPE1)和第5行(TYPE2),第6,7行(TYPE1)和第8,9行(TYPE2)的分组。

这可以在一个正则表达式中完成,或者我应该为每种类型创建一个表达式,然后逐行检查,如果前一行是相同的类型?

最后,我需要返回一个包含pair(int start_char, int end_char)

我可以基于行模式用Regex对文本文件进行分组吗?

的分组列表。

你可以试试这个

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 });