I'm writing a program to scan a text file for blocks of strings (lines) and output the blocks to a file when found
In my process class, the function proc() is taking an unusually long time to process a 6MB file. On a previous program I wrote where I scan the text for only one specific type of string it took 5 seconds to process the same file. Now I rewrote it to scan for the presence of different strings. it is taking over 8 minutes which is a significant difference. Does any one have any ideas how to optimize this function?

This is my RegEx 

```
System.Text.RegularExpressions.Regex RegExp { get { return new System.Text.RegularExpressions.Regex(@"(?s)(?-m)MSH.+?(?=[\r\n]([^A-Z0-9]|.{1,2}[^A-Z0-9])|$)", System.Text.RegularExpressions.RegexOptions.Compiled); } }

```

.

```
 public static class TypeFactory
{
    public static List<IMessageType> GetTypeList()
    {
        List<IMessageType> types = new List<IMessageType>();
        types.AddRange(from assembly in AppDomain.CurrentDomain.GetAssemblies()
                       from t in assembly.GetTypes()
                       where t.IsClass && t.GetInterfaces().Contains(typeof(IMessageType))
                       select Activator.CreateInstance(t) as IMessageType);

        return types;
    }
}

public class process
{

    public void proc()
    {
        IOHandler.Read reader = new IOHandler.Read(new string[1] { @"C:\TEMP\DeIdentified\DId_RSLTXMIT.LOG" });

        List<IMessageType> types = MessageType.TypeFactory.GetTypeList();

        //TEST1
        IOHandler.Write.writeReport(System.DateTime.Now.ToString(), "TEST", "v3test.txt", true);

        foreach (string file in reader.FileList)
        {
            using (FileStream readStream = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                int charVal = 0;
                Int64 position = 0;
                StringBuilder fileFragment = new StringBuilder();
                string message = string.Empty;

                string current = string.Empty;
                string previous = string.Empty;

                int currentLength = 0;
                int previousLength = 0;

                bool found = false;

                do
                {
                    //string line = reader.ReturnLine(readStream, out charVal, ref position);
                    string line = reader.ReturnLine(readStream, out charVal);

                    for (int i = 0; i < types.Count; i++)
                    {
                        if (Regex.IsMatch(line, types[i].BeginIndicator)) //found first line of a message type
                        {
                            found = true;
                            message += line;

                            do
                            {
                                previousLength = types[i].RegExp.Match(message).Length;

                                //keep adding lines until match length stops growing
                                //message += reader.ReturnLine(readStream, out charVal, ref position);
                                message += reader.ReturnLine(readStream, out charVal);
                                currentLength = types[i].RegExp.Match(message).Length;

                                if (currentLength == previousLength)
                                {
                                    //stop - message complete
                                    IOHandler.Write.writeReport(message, "TEST", "v3test.txt", true);

                                    //reset
                                    message = string.Empty;
                                    currentLength = 0;
                                    previousLength = 0;
                                    break;
                                }

                            } while (charVal != -1);

                            break;
                        }
                    }

                } while (charVal != -1);

                //END OF FILE CONDITION
                if (charVal == -1)
                {

                }

            }

        }
        IOHandler.Write.writeReport(System.DateTime.Now.ToString(), "TEST", "v3test.txt", true);
    }
}

```

.

EDIT: I ran profiling wizard in VS2012 and I found most time was spent on RegEx.Match function