AdityaK
AdityaK

Reputation: 339

Getting whole Line containing specific word using Regular Expression C#

I have a txt file containing sentences like:

When I move to this page
Then It display poup
......
......

so i want to get lines only contains "Then" keyword at starting using Regular Expression

Upvotes: 1

Views: 775

Answers (2)

Selman Genç
Selman Genç

Reputation: 101731

This can be easily done using string.StartsWith method via LINQ:

var lines = File.ReadLines("path")
                .Where(line => line.StartsWith("Then"))
                .ToList();

Upvotes: 2

Jonesopolis
Jonesopolis

Reputation: 25370

You don't want or need Regex for this:

var lines = File.ReadAllLines(filePath).Where(l => l.StartsWith("Then"));

Upvotes: 1

Related Questions