删除重复的单词

时间:2019-11-18 15:49:30

标签: c# regex

如何替换字符串末尾的重复单词?这段代码成功替换了中间的重复项(后跟空格,标点符号或CRLF)。

Console.WriteLine(Regex.Replace("In the the beginning", @"(?'dupe'\w+[\s\.\r\n])\k'dupe'", "${dupe}")); // In the beginning

但是,如果开始写两次,它将不会被替换:

Console.WriteLine(Regex.Replace("In the the beginning beginning", @"(?'dupe'\w+[\s\.\r\n])\k'dupe'", "${dupe}")); // In the beginning beginning

那是因为正则表达式引擎将字符串“ beginning”存储在dupe组中,而不仅仅是“ beginning”。通常的解决方法是什么?

0 个答案:

没有答案
相关问题