Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0642 Character Escapes

Regular expressions have the following metacharacters: \ * + ? | { [ () ^ $ . # To refer to a metacharacter literally, you must prefix the character with a backslash. In the following example, we escape the ? character to match the string "what?": using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { Console.WriteLine(Regex.Match("what?", @"what\?")); Console.WriteLine(Regex.Match("what?", @"what?")); } } The output: what? what