替换字符串中的单个字符实例,保持多个字符不变

时间:2018-04-12 04:55:32

标签: c# regex

如何将periods替换为spaces,但保留...

string test = "This.is.a.test...";

test = test.Replace(".", " ");

http://rextester.com/DLEHI1253

1 个答案:

答案 0 :(得分:5)

您可以使用此(?<!\.)\.(?!\.)

var regex = new Regex(@"(?<!\.)\.(?!\.)");
var ressult = regex.Replace("This.is.a.test..."," ");
Console.WriteLine(ressult);

输出

This is a test...

Demo here