Tag Archives: Regex

Regex in C#

Remove empty spaces from string

string sentence = " this is      a sentence with many     empty spaces      ";
sentence = sentence.Trim();
sentence = Regex.Replace(sentence, @"\s+", " "):

Regex matching

Regex regex = new Regex(@"@@\S+");
Match match = regex.Match("this is a @@specialword in my string");

if (match.Success)
  Console.WriteLine("Regex found: " + match.Value);

Test your Regex

A good site to get an overview of what Regex has to offer and directly test it is https://regex101.com/