trystring yourStr = "this is a white cat;"; string result = "and the cat is lovely."; List<string> list = new List<string>(yourStr.Split(' ')); string temp = ""; foreach (string s in list) { temp = Regex.Replace(s, @"\W",""); result = Regex.Replace(result, @"\b" + temp + @"\b", ""); }richTextBox2.Text = result;
拿句2中的每个词与句1匹配,匹配上继续句2中的下一个单词,匹配不上把这个单词放到句1末尾,这样把句2扫描一遍就完了。
正则表达式似乎搞不定,当然在匹配的时候可选正则表达式。
其复杂度也不过跟选择排序差不多。
string result = "and the cat is lovely.";
List<string> list = new List<string>(yourStr.Split(' '));
string temp = "";
foreach (string s in list)
{
temp = Regex.Replace(s, @"\W","");
result = Regex.Replace(result, @"\b" + temp + @"\b", "");
}richTextBox2.Text = result;