In a .NET Regex
pattern, what special characters need to be escaped in order to be used literally?
问题:
回答1:
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);
}
回答2:
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.
回答3:
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#$()*+.?[\^{|
回答4:
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 (?=)
.