使字符串检查更有效

本文关键字:有效 检查 字符串 | 更新日期: 2023-09-27 17:50:41

我使用以下代码检查一个字符串是否包含在另一个字符串

foreach (string testrecord in testlist)
{
   foreach (string realrecord in reallist)
   {         
      if ((Regex.Replace(testrecord , "[^0-9a-zA-Z]+", "")
                .Contains((
                    Regex.Replace(realrecord, "[^0-9a-zA-Z]+", ""))) 
          && 
           ((Regex.Replace(realrecord, "[^0-9a-zA-Z]+", "") != "") 
          && 
           ((Regex.Replace(realrecord, "[^0-9a-zA-Z]+", "").Length >= 4)))))
      {
         matchTextBox.AppendText("Match: " + testrecord + " & " + realrecord + Environment.NewLine);
      }
   }
}

然而,完成此操作的运行时间需要相当长的时间。由于我添加了特殊字符regex移除,运行时花费了更长的时间,但regex是绝对需要的。

是否有更有效的方法来应用这个正则表达式?我试图将它添加到foreach字符串变量,但是你不能改变它们,因为它们在foreach循环中。

使字符串检查更有效

优化版:

// Do not put text into matchTextBox direct:
// it makes the control re-painting each time you change the text
// Instead, collect all the text into StringBuffer  
StringBuilder Sb = new StringBuilder(); 
// Pull out as much as you can from the inner loop,
// that's why I've changed the loops' order:
// first loop on reallist, then on testlist
foreach (string realrecord in reallist) {
  // Cache Regex.Replace result
  String realCleaned = Regex.Replace(realrecord, "[^0-9a-zA-Z]+", "");
  // Test as early as possible
  if (realCleaned.Length < 4)
    continue;
  // You don't need to test realCleaned != "";: realCleaned.Length < 4 is enough
  foreach (string testrecord in testlist) {
    // Cache Regex.Replace result: it's a little bit overshoot here, but if some
    // more tests are added it'll be helpful
    String testCleaned = Regex.Replace(testrecord, "[^0-9a-zA-Z]+", "");
    if (testCleaned.Contains(realCleaned))
      Sb.AppendLine("Match: " + testrecord + " & " + realrecord);
  }  
}
// At last matchTextBox.Text change
matchTextBox.AppendText(Sb.ToString());

这应该更快一点(每个testrecord一个regex操作):

var strippedRealList = reallist.Select(s => Regex.Replace(s, "[^0-9a-zA-Z]+", ""))
                               .Where(s => s.Length >= 4)
                               .ToArray();
foreach (string realrecord in reallist)
{
   strippedRealList.Where(s => realrecord.Contains(s))
                   .ToList()
                   .ForEach(s =>
                            matchTextBox.AppendText("Match: "
                                                  + s
                                                  + " & "
                                                  + realrecord
                                                  + Environment.NewLine));
}

我想知道你正在使用Regex来实现你的目的,而忽略了这样一个事实,即你也可以通过只使用。contains()方法来实现这一点,这样你的代码应该比

之前更简单、更快。
foreach (string testrecord in testlist)
{
   foreach (string realrecord in reallist)
   {         
      if(testrecord.Contains(realrecord))
         {
         matchTextBox.AppendText("Match: " + testrecord + " & " + realrecord + Environment.NewLine);
         }
   }
}