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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s