Final Corrected Code Block: var rx = new Regex(@"(?[^\r\n]+)(?\r\n|\r|\n)?"); foreach (Match m in rx.Matches(str31)) { var line0 = m.Groups["line"].Value; var eol0 = m.Groups["eol"].Value; switch (eol0) { case "\r\n": // Proper CRLF – no issue break; case "\n": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing CR for the line below:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; case "\r": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing LF for the line below:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; case "": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("The line below is missing both LF and CR:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; } } This ensures: No false positives for empty lines. Correct detection of missing CR, LF, or both. Handles last line without newline properly. Here’s a quick C# test snippet you can use to verify the corrected regex and logic: using System; using System.IO; using System.Text.RegularExpressions; class Program { static void Main() { string str31 = @"H+CNQIZTQZ+ARRI+250729+0920+Z1.0++++TQZ L+SEGU2150230+250723+1502+BACK+NIL+E++++CNQIZCLT++CNQIZTQZ+++++ZIM+ZIM++^^^^^^N^^^^^^^^^^^^^^^^^1^^^^+^^^^^^^^^^+^ X+TEST+LINE+WITH+CRLF\r\n Y+TEST+LINE+WITH+LF\n Z+TEST+LINE+WITH+CR\r"; using (var sw = new StringWriter()) { bool b_err3 = false; string fileName = "SampleFile.txt"; var rx = new Regex(@"(?[^\r\n]+)(?\r\n|\r|\n)?"); foreach (Match m in rx.Matches(str31)) { var line0 = m.Groups["line"].Value; var eol0 = m.Groups["eol"].Value; switch (eol0) { case "\r\n": break; // Proper CRLF case "\n": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing CR for the line below:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; case "\r": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("It is missing LF for the line below:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; case "": if (!b_err3) { sw.WriteLine($"Please check the lines having the issues below in the file - '{fileName}'."); sw.WriteLine(); } sw.WriteLine("The line below is missing both LF and CR:"); sw.WriteLine(line0); sw.WriteLine(); b_err3 = true; break; } } Console.WriteLine(sw.ToString()); } } } ``Expected Output: Please check the lines having the issues below in the file - 'SampleFile.txt'. The line below is missing both LF and CR: H+CNQIZTQZ+ARRI+250729+0920+Z1.0++++TQZ The line below is missing both LF and CR: L+SEGU2150230+250723+1502+BACK+NIL+E++++CNQIZCLT++CNQIZTQZ+++++ZIM+ZIM++^^^^^^N^^^^^^^^^^^^^^^^^1^^^^+^^^^^^^^^^+^ It is missing CR for the line below: Y+TEST+LINE+WITH+LF It is missing LF for the line below: Z+TEST+LINE+WITH+CR v