.NET Regex
escape characters
regular expressions
regex syntax
programming tips

What characters need to be escaped in .NET Regex?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET regular expressions, characters only need escaping when the regex engine would otherwise treat them as syntax instead of literal text. The tricky part is that escaping happens at two levels in C#: first in the regex language, and sometimes again in the C# string literal that contains the pattern.

The Main Regex Metacharacters

Outside a character class, the most important regex metacharacters are:

  • '.'
  • '^'
  • '$'
  • '*'
  • '+'
  • '?'
  • '('
  • ')'
  • '['
  • ']'
  • '{'
  • '}'
  • '|'
  • '\'

If you want one of those to mean itself literally, escape it with a backslash in the regex pattern.

For example, to match a literal dot:

csharp
1using System.Text.RegularExpressions;
2
3var ok = Regex.IsMatch("file.txt", @"file\.txt");
4System.Console.WriteLine(ok);

Without the backslash, . would mean "any character."

Character Classes Have Different Rules

Inside square brackets, the rules change. In a character class, some characters lose their usual meaning, while others become special only in that context.

Important special cases inside a character class include:

  • '] to close the class'
  • '- for ranges such as a-z'
  • '^ at the start to negate the class'
  • '\ for escapes'

Example:

csharp
1using System.Text.RegularExpressions;
2
3var pattern = @"^[A-Z0-9\-]+$";
4var ok = Regex.IsMatch("AB-19", pattern);
5System.Console.WriteLine(ok);

Here the hyphen is escaped so it is treated literally instead of as a range operator.

C# String Escaping Versus Regex Escaping

This is where many developers get confused. In a normal C# string literal, backslash is also an escape character for the language itself.

These two patterns are equivalent:

csharp
var pattern1 = "\\d+";
var pattern2 = @"\d+";

The second version uses a verbatim string literal, which is usually easier to read for regex code. A useful rule is:

  • use verbatim strings for most regex patterns
  • then think only about regex escaping, not C# escaping

Use Regex.Escape for User-Supplied Literal Text

If the text comes from a user or another external source and should be treated literally, do not hand-escape it. Use Regex.Escape.

csharp
1using System;
2using System.Text.RegularExpressions;
3
4var literal = "a+b?.txt";
5var pattern = Regex.Escape(literal);
6
7Console.WriteLine(pattern);
8Console.WriteLine(Regex.IsMatch("a+b?.txt", pattern));

This is much safer than trying to remember every special character manually.

Examples of Common Literal Matches

Literal parentheses:

csharp
var ok = Regex.IsMatch("(test)", @"\(test\)");

Literal backslash:

csharp
var ok = Regex.IsMatch(@"C:\Temp", @"C:\\Temp");

Literal currency amount with a dot:

csharp
var ok = Regex.IsMatch("$19.99", @"^\$19\.99$");

Each example shows the same principle: escape only the characters that would otherwise act like regex syntax.

Do Not Over-Escape Randomly

Over-escaping can make patterns harder to read and sometimes changes meaning. For example, many ordinary letters and digits do not need escaping in most regex contexts.

A better habit is:

  1. identify whether the character is special in the current regex context
  2. escape it only if needed
  3. use Regex.Escape when the whole input is meant literally

That produces patterns that are easier to maintain.

Common Pitfalls

The most common mistake is forgetting that a C# string literal and a regex pattern have separate escaping rules. Developers often fix one layer and forget the other.

Another issue is assuming the same escape rules apply inside and outside character classes. They do not.

A third problem is manually escaping user input instead of using Regex.Escape, which is easy to get wrong and harder to audit.

Summary

  • Escape regex metacharacters when you need them to be matched literally.
  • Remember that character classes have different special-character rules.
  • In C#, prefer verbatim string literals for regex patterns.
  • Use Regex.Escape for user-supplied literal text.
  • Distinguish carefully between regex escaping and C# string escaping.

Course illustration
Course illustration

All Rights Reserved.