Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除字符串C#/Regex中连续出现的模式_C#_Regex - Fatal编程技术网

如何删除字符串C#/Regex中连续出现的模式

如何删除字符串C#/Regex中连续出现的模式,c#,regex,C#,Regex,我正在研究一个使单词和句子正常化的问题 例如Yahooo->Yahoo 是的->是的 哈哈哈->哈哈哈 你好世界你好世界->你好世界 基本上,任何出现2次以上的模式都需要标准化为一次出现 Edit1:基于以下问题,我将添加更多案例 “你好,世界你好,你好,嘿嘿嘿嘿嘿嘿”——“你好,世界你好,你好,他” “你好世界你好世界”——“你好世界” “aaabbb aaabbb aaabbb”-“ab ab”-“ab”(最终输出) 因此,条件是出现2次以上,所有过滤器(单字符、双字符、三字符直到5字符)都

我正在研究一个使单词和句子正常化的问题

例如Yahooo->Yahoo

是的->是的

哈哈哈->哈哈哈

你好世界你好世界->你好世界

基本上,任何出现2次以上的模式都需要标准化为一次出现

Edit1:基于以下问题,我将添加更多案例


“你好,世界你好,你好,嘿嘿嘿嘿嘿嘿”——“你好,世界你好,你好,他”

“你好世界你好世界”——“你好世界”

“aaabbb aaabbb aaabbb”-“ab ab”-“ab”(最终输出)


因此,条件是出现2次以上,所有过滤器(单字符、双字符、三字符直到5字符)都应该应用。

您可以使用以下代码:

string input = @"hello world hello hello hehehehehehehehehe aaabbb aaabbb aaabbb";

while(input.Length != (input = Regex.Replace(input, @"(.+)\1{2,}", "$1")).Length);

Console.WriteLine(input);
使用Regex
(.+)\1{2,}
表示匹配重复两次或多次的一个或多个字符。它产生以下输出:

你好,世界你好,你好,他是ab


关于
“hello world hello hello heheheheheheheheheh”
为什么不
“hello world hello world hello world”
“helo world”
(因为
“l”
也是重复的)?关于
aaabbb aaabbb aaabbb
呢?如何确定模式发生的结束时间?如果重复的项目不是连续的,会发生什么情况?我不是regex专家,但我认为这在regex中是不可能的(即使是这样,用逻辑实现它可能比用regex容易得多)。“hello world hello heheheheheheheheheh”-“hello world hello Hehehehehehehehehe”。“你好世界你好世界”——“你好世界”。“aaabbb aaabbb aaabbb”-“ab ab”-“ab”(最终输出)。因此,条件是出现次数大于2次,并且应应用所有过滤器(单字符、双字符、三字符,直到5字符)。