使字符串检查更有效

时间:2014-03-17 13:21:00

标签: c# regex string

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

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

      }
   }

}

然而,完成此操作的运行时间需要很长时间。由于我添加了特殊字符正则表达式删除,因此运行时间要长得多,但绝对需要正则表达式。

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

3 个答案:

答案 0 :(得分:3)

优化版本:

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

答案 1 :(得分:1)

这应该更快一点(每testrecord一个正则表达式操作):

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

}

答案 2 :(得分:-1)

我想知道你正在使用正则表达式实现你的目的,忽略了你也可以通过只使用.Contains()方法实现这一目的,这样你的代码应该比之前更简单,更快

foreach (string testrecord in testlist)
{
   foreach (string realrecord in reallist)
   {         
      if(testrecord.Contains(realrecord))
         {
         matchTextBox.AppendText("Match: " + testrecord + " & " + realrecord + Environment.NewLine);
         }
   }

}