Home > Back-end >  C# - Replace all occurrences of 3 or more periods in a string
C# - Replace all occurrences of 3 or more periods in a string

Time:03-17

I'm cleaning a lot of text files and I am finding that through this cleaning, I am getting a lot of sequential periods (.) in some of the results.

The number of periods occurring can range from 3 to theoretically thousands. How can I replace all occurrences of more than 3 periods in a row with 1 period? I have heard that Regex is the best way to do this, but I am terrible with Regex.

CodePudding user response:

Use this:

var result = Regex.Replace(myInput, "\\.{3,}", ".");

This will replace all occurences of at least 3 dots by a single one.

Simple demo? See my fiddle: https://dotnetfiddle.net/Mdcnc9

CodePudding user response:

string output = Regex.Replace(input, @"(\.\.\.\. )", ".");
  • Related