合并两个字符串,替换重复的内容

时间:2018-02-21 16:53:02

标签: c# regex

已尝试搜索答案但未找到任何匹配的用例。

目标:合并两个字符串,替换重复的短语/内容。

示例..

String first =  "The quick brown fox jumped.";
String second = "The quick brown fox jumped, and was happy.";
// magic goes here
String intended_outcome = "The quick brown fox jumped, and was happy";

所以在这种情况下它很简单,可以只做一个String.IndexOf nocase序数。

但后来考虑一下:

String first =  "Hello there. The quick brown fox jumped.";
String second = "The quick brown fox jumped, and was happy.";

// remove all punctuation, words by spaces only(to simplify)
first = Regex.Replace(first,@"[^\w\s]",""); 
second = Regex.Replace(second,@"[^\w\s]","");
// magic goes here

String intended_outcome = "Hello there, The quick brown fox jumped, and was happy";
  • 你会建议我转换为一个数组(first+second合并,按空格分割),然后递归处理以逐个单步执行每个单词来查找重复的短语吗?
  • 或者循环每个单词,使用indexOf检查来查找两者之间的常用单词(例如。second.indexOf(av_split_first[i])!=-1),作为起始索引,并处理后续单词,直到找到所有单词,替换并递归w / offset?

以前有人解决过这个问题吗? 我的目标是C#,但对任何语言,正则表达式或其他方面的任何解决方案感兴趣

0 个答案:

没有答案