Matt McManis
Matt McManis

Reputation: 4675

Replace single instance of character in a string, leave multiple characters untouched

How would I replace periods with spaces, but preserve ...?

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

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

http://rextester.com/DLEHI1253

Upvotes: 2

Views: 58

Answers (1)

TheGeneral
TheGeneral

Reputation: 81493

You could use this (?<!\.)\.(?!\.)

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

Output

This is a test...

Demo here

Upvotes: 5

Related Questions