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:
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 asa-z' - '
^at the start to negate the class' - '
\for escapes'
Example:
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:
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.
This is much safer than trying to remember every special character manually.
Examples of Common Literal Matches
Literal parentheses:
Literal backslash:
Literal currency amount with a dot:
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:
- identify whether the character is special in the current regex context
- escape it only if needed
- use
Regex.Escapewhen 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.Escapefor user-supplied literal text. - Distinguish carefully between regex escaping and C# string escaping.

