In a .NET Regex
pattern, what special characters need to be escaped in order to be used literally?
4 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);
}
-
1Escape $ character too?:
var needle = @"any text here$%# more text";
– KiquenetDec 18, 2014 at 12:50 -
4@JonSkeet: thanks for pointing out Regex.Escape, I was about to re-invent a square wheel till i saw your answer.– mcdonApr 8, 2015 at 14:05
-
1@JonSkeet : I know it is late, but why not to do it to containment? I have to use .net 1.1, so I can't use String.Contains, and equivalents Aug 18, 2016 at 19:21
-
2
-
2Also, interestingly, this does NOT escape "-" (hyphen), which causes issues in character classes. I guess convention is to place hyphen first in a character class so most people won't have issues like I did with what ended up being a string like this: $@"[a-zA-Z0-9{System.Text.RegularExpressions.Regex.Escape("_-$}")}]" which caused a somewhat confusing "parsing "[a-zA-Z0-9_-$]" - [x-y] range in reverse order." ArgumentException.– csrowellJun 4, 2018 at 16:11
Here is the list of characters that need to be escaped to use them as normal literals:
- Opening square bracket
[
- Backslash
\
- Caret
^
- Dollar sign
$
- Period or dot
.
- Vertical bar or pipe symbol
|
- Question mark
?
- Asterisk or star
*
- Plus sign
+
- Opening round bracket
(
and the closing round bracket)
- Opening curly bracket
{
- 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.
-
6
-
1
-
1As JDB said, it depends on context. For example,
]
is not on the list but needs to be escaped if used as a literal and preceded by[
. Aug 2, 2019 at 19:55 -
1I get an error if I try to escape asterisk or star * but seems to be working if I don't escape it.– JonAug 21, 2019 at 20:38
-
1Why do you need to escape the octothorpe ('#') symbol? That appears to be an error in the above list. Dec 12, 2019 at 15:18
See the MSDN documentation here: http://msdn.microsoft.com/en-us/library/az24scfc.aspx#character_escapes
The problem with a complete list is that it depends on context. For example .
must be escaped, unless it is enclosed in brackets, as in [.]
. ]
technically does not need to be escaped, unless it is preceded by [
. -
has no special meaning, unless it's inside of brackets, as in [A-Z]
. =
has no special meaning unless it is preceded by ?
as in (?=)
.
I think you can get the list of chars as
List<char> chars = Enumerable.Range(0,65535)
.Where(i=>((char)i).ToString()!=Regex.Escape(((char)i).ToString()))
.Select(i=>(char)i)
.ToList();
--
\t\n\f\r#$()*+.?[\^{|