How do i Remove a tab or space from a Read Line Using Regular Expression
<p>i am creating a Log Parser whereby i Load a log file and Parse it by going through the log file one line at a time. I am using Regular Expression to parse my Files for a specific Pattern. The problem i am having is that in the actual log file there is a tab or space before or in front of the selected line and my data will not match my comparison data. </p>

<p>How do i remove the Tab or white space in front of the line. I tried Trim() and trimStart() that did not work. Here is my sample codes:</p>

<p>Log File Sample pattern:
[ANY__MOD,80,*AUDIT_GROUPS*] [ANY__MOD] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.</p>

<p>Regex Pattern i have for this:
string Pattern = @"/.<em>[([.\d\s\w*]</em>),([.\d\s\w*]<em>),([.\d\s\w*]</em>)]\s*[([.\d\s\w*]<em>)]\s</em>(.)*";</p>

<p>Problem i am having:"<strong>\t</strong>[.22.12.,81,*AUDIT_GROUPS*] [.22.12.] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.</p>

<pre><code> [.22.12.,81,*AUDIT_GROUPS*] [.22.12.] audit_groups_category Key1 contains invalid value. Replace dictionary dat file with one having valid Key1 value ANY_MOD.
</code></pre>

<p>My Sample Code:</p>

<pre><code>private void ValidateUsingRegularExpression(string Pattern,  string serviceName)
       {
           System.Diagnostics.Debugger.Launch();

           string line;
           int counter = 0;
           string ServiceName = Helpers.GetServiceName(serviceName, _integrationType);
           string serviceLogPath = Helpers.GetTppInstallDir() + "logs\\" + ServiceName + ".txt";
         //  string serviceLogPath = @"C:\totalpayment\logs\EngineService.txt";
         // If file does not exist
            if(!File.Exists(serviceLogPath))
           {
            throw new ApplicationException("Was unable to find Log file " + serviceLogPath );
           }

          System.IO.StreamReader file = new System.IO.StreamReader(serviceLogPath);

           line = file.ReadLine();

           while (line != null)
           {
               line = file.ReadLine();
               if (line == null)
               {
                   return;
               }
               line = line.Trim();

                   if (Regex.IsMatch(line, Pattern, RegexOptions.IgnoreCase))
                   {
                       line = line.Trim();
                       line = line.TrimStart();
                       if (ErrorToValidate == line)
                       {
                           _ValidateErrorinLogFile = "Found";
                           counter = counter + 1;
                          // counter++;
                           _TotalLinesFound = counter.ToString();
                       }

                   }
           }
       }
</code></pre>

<p>main function that calls</p>

<pre><code>public void  ValidateCompareExclusionErrorsInLogFile()
       {

          // string Pattern = @"\[\d+\,[A-Z_]+\]
          // string Pattern = @"\[([\w\*]+),(\d+),([\w\*]+)\]\s*\[([\w\*_]+)\]\s*([\w\\d\s\.]*)";
           //string Pattern = \[([\w\*]+),(\d+),([\w\*]+)\]\s*\[([\w\*\_]+)\]\s*([\w\\d\s\.]*);

           string Pattern = @"/.*\[([.\d\s\w\*]*),([.\d\s\w\*]*),([.\d\s\\w\*]*)\]\s*\[([.\d\s\w\*]*)\]\s*(.)*";

           //ValidateUsingRegularExpression(Pattern, "TPP.EngineService");
           ValidateUsingRegularExpression(Pattern, "engine");
           ValidateUsingString(ErrorToValidate, "engine");
           //These options are here when needed,  If you need to setup different suite of test to run same scenario on different engine, 
           //this can be dynamically done by turning on the following options below. 
            //  ValidateUsingString("field","engine");

       }
</code></pre>
