Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What characters need to be escaped in .NET Regex?

In a .NET Regex pattern, what special characters need to be escaped in order to be used literally?

like image 389
Eric Avatar asked Oct 18 '12 20:10

Eric


People also ask

What characters have to be escaped regex?

Operators: * , + , ? , | Anchors: ^ , $ Others: . , \ In order to use a literal ^ at the start or a literal $ at the end of a regex, the character must be escaped.

Do I need to escape period in regex?

(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \. accordingly.

Do we need to escape Colon in regex?

Colon does not have special meaning in a character class and does not need to be escaped.

What is escaping in regex?

The backslash in a regular expression precedes a literal character. You also escape certain letters that represent common character classes, such as \w for a word character or \s for a space.


2 Answers

I don't know the complete set of characters - but I wouldn't rely on the knowledge anyway, and I wouldn't put it into code. Instead, I would use Regex.Escape whenever I wanted some literal text that I wasn't sure about:

// Don't actually do this to check containment... it's just a little example. public bool RegexContains(string haystack, string needle) {     Regex regex = new Regex("^.*" + Regex.Escape(needle) + ".*$");     return regex.IsMatch(haystack); } 
like image 128
Jon Skeet Avatar answered Oct 17 '22 07:10

Jon Skeet


Here is the list of characters that need to be escaped to use them as normal literals:

  1. Opening square bracket [
  2. Backslash \
  3. Caret ^
  4. Dollar sign $
  5. Period or dot .
  6. Vertical bar or pipe symbol |
  7. Question mark ?
  8. Asterisk or star *
  9. Plus sign +
  10. Opening round bracket ( and the closing round bracket )
  11. Opening curly bracket {
  12. Pound/Hash sign #

These special characters are often called "metacharacters".

But, I agree with Jon to use Regex.Escape instead of hardcoding these character in code.

like image 31
Megha Jaiswal Avatar answered Oct 17 '22 08:10

Megha Jaiswal