C# 如何使用正则表达式从读取行中删除制表符或空格

C# 如何使用正则表达式从读取行中删除制表符或空格,c#,C#,我正在创建一个日志解析器,通过这个解析器,我可以加载一个日志文件,并通过一次一行地遍历日志文件来解析它。我正在使用正则表达式为特定模式解析文件。我遇到的问题是,在实际日志文件中,在所选行之前或之前有一个选项卡或空格,我的数据与比较数据不匹配 如何删除行前面的制表符或空白。我尝试了Trim()和trimStart(),但没有成功。以下是我的示例代码: 日志文件示例模式: [ANY__MOD,80,*审核组*][ANY_MOD]审核组类别键1包含无效值。将字典dat文件替换为具有有效Key1值ANY

我正在创建一个日志解析器,通过这个解析器,我可以加载一个日志文件,并通过一次一行地遍历日志文件来解析它。我正在使用正则表达式为特定模式解析文件。我遇到的问题是,在实际日志文件中,在所选行之前或之前有一个选项卡或空格,我的数据与比较数据不匹配

如何删除行前面的制表符或空白。我尝试了Trim()和trimStart(),但没有成功。以下是我的示例代码:

日志文件示例模式: [ANY__MOD,80,*审核组*][ANY_MOD]审核组类别键1包含无效值。将字典dat文件替换为具有有效Key1值ANY_MOD的文件

 [.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.
我对此有一个正则表达式模式: 字符串模式=@/[([.\d\s\w*]),([.\d\s\w*]),([.\d\s\w*])]\s*[([.\d\s\w*])]\s(.*”

我遇到的问题:“\t[.22.12,81,*审核组*][.22.12.]审核组类别键1包含无效值。请将字典dat文件替换为具有任何有效键1值的文件

 [.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.
我的示例代码:

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();
                       }

                   }
           }
       }
调用

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");

       }

这适用于您提供的数据:

^\s+\[[^]]+]\s*\[[^]]+\].+$
这就是它的意思:

^ (anchor to start of string)
Any whitespace character 
+ (one or more times)
[
Any character not in "]"
+ (one or more times)
]
Any whitespace character 
* (zero or more times)
[
Any character not in "]"
+ (one or more times)
]
. (any character)
+ (one or more times)
$ (anchor to end of string)

感谢您的反馈,但是即使我按照您的建议执行了操作,代码中的制表符或空格仍然存在问题。再次感谢您的反馈非常感谢您的反馈非常有效,我的问题得到了解决。非常感谢您在这方面的帮助。@user1097970-np。很高兴我能提供帮助。我没有关于r但是
Trim()
将删除字符串开头和结尾的空白,包括制表符。因此,请仔细检查围绕此的逻辑。另外,您如何知道它开头是制表符,它可能是另一个字符?