日期:2014-05-17  浏览次数:20930 次

C# 求个正则或其他高性能算法:判断母字符串是否存在字符串(包括相似,即允许误差)
C# code
str1="xxxxxxxxxxcsdnnetxxxxxxxxxxxxxx";
str2="xxxxxxccdnnetxxxxxxxx";
str3="xxxxxxxxxxxxxxxxcsdnnatxxxxx";
str4="xxxxxxxxcssnnatxxxxxxxxxxxxxxxx";

C# code
tmpstr="csdnnet";



"X"是内容随机的字符串,判断 strX 里是否含有 tmpstr(允许错一个字符),即以上除str4外(str4里错了两个字符),其他三个str全部输出"OK"或者输出错误数量,即输出结果:" str1完整包函,str2 str3错一,str4 错2"! 求高强大的网友们赐个代码,谢谢了!

------解决方案--------------------
C# code

void Main()
{
string    str1="xxxxxxxxxxcsdnnetxxxxxxxxxxxxxx";
string    str2="xxxxxxccdnnetxxxxxxxx";
string    str3="xxxxxxxxxxxxxxxxcsdnnatxxxxx";
string    str4="xxxxxxxxcssnnatxxxxxxxxxxxxxxxx";
Console.WriteLine("{0}:\t{1}",str1,CheckString(str1));
Console.WriteLine("{0}:\t{1}",str2,CheckString(str2));
Console.WriteLine("{0}:\t{1}",str3,CheckString(str3));
Console.WriteLine("{0}:\t{1}",str4,CheckString(str4));
  
  /*
  xxxxxxxxxxcsdnnetxxxxxxxxxxxxxx:    OK
xxxxxxccdnnetxxxxxxxx:    OK
xxxxxxxxxxxxxxxxcsdnnatxxxxx:    OK
xxxxxxxxcssnnatxxxxxxxxxxxxxxxx:    ERROR

  */
   
}
 string[] temp=new string[]{"csdnnet","[a-z]sdnnet","c[a-z]dnnet","cs[a-z]nnet"
   ,"csd[a-z]net","csdn[a-z]et","csdnn[a-z]t","csdnne[a-z]"};
   
   
string CheckString(string str)
{
   string result="ERROR";
  Regex reg;
    foreach(string s in temp)
    {
      reg=new Regex(s);
      if(reg.IsMatch(str))
        {
            result="OK"; 
            break;
        }
    }
  
  return result;
}