SkipWhile TakeWhile,然后在上方也行直到比赛

I'm using SkipWhile and TakeWhile to gather lines from a large file using Linq.

var foundLines = File.ReadLines(filePath).SkipWhile(line => !line.Contains("pattern1")).TakeWhile(line => !line.Contains("pattern2"));

但是我还希望文本在我收集的行上方,直到另一个字符串匹配为止。这可能吗?

更多信息:我首先搜索一个ID。然后,我需要在ID上方输入一些文字。还需要此文本以了解我最初搜索的内容。

我将尝试以视觉方式显示此内容。我只有pattern1,这个变量是给定的。 Pattern2和pattern3是静态数据。

...
...
...pattern3...
...
...
...pattern1...
...
...
...pattern2...
...
...

The text between pattern1 and pattern2 is what I can simply catch by doing SkipWhile and TakeWhile on File.ReadLines. After this is catched, I need to go up and catch all data until pattern3.

So the whole data I want to catch is from pattern3 until pattern2. But my starting point must be pattern1 because that info (ID) is given and data changes based on that.

例如:

Pattern1 is an ID, or a GUID: KFK284NSKQLOFIE8.

Pattern2 is "end processing"

Pattern3 is "start processing"

我需要“开始处理”和“结束处理”之间的所有行

If this is not possible with Linq, how can I solve this problem? Would FileStream.Seek be an option?

评论