I am trying to read the last serial number in a large text file with the code below using Regex. There is two spaces before and after the serial numbers in the text file on at the beginning of each line of text. This takes quite a long time if the file is too big. Is it possible to read the text file from the end of the file to the beginning so that the first capture alone with Match will get me the answer and reduce the time taken in c#. Thanks in advance.
string contents = File.ReadAllText(path);
string pattern = @"(?<=\s{2}\d{1,7}(?=\s{2})";
MatchCollection matches = Regex.Matches(contents, pattern);
string lastmatch = string.Empty;
foreach (Match s in matches)
{
lastmatch = s.Groups[0].ToString();
}
MessageBox.Show(lastmatch);
The text file looks like.
1 Blah Blah Blah.
2 Ding Dong Bell.
3 Hello, how are you.
4 My name is Unnikrishnan.
5 You are a very good friend.