Performance enhancement for Issues/#11877

There is no need to recompile the pattern for every line in the file. This version behaves identically, but moves the re.compile(pat) call out of the line comparison loop.