Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0651 Anchors

The anchors ^ and $ match a particular position. By default: ^ Matches the start of the string $ Matches the end of the string ^ has two context-dependent meanings: an anchor and a character class negator. $ has two context-dependent meanings: an anchor and a replacement group denoter. using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { Console.WriteLine (Regex.Match ("Javascript", "^[J]a")); Console.WriteLine (Regex.Match ("C#", "[Cc]$")); } } The output: Ja If you specify RegexOptions.Multiline or include (?m) in the expression: ^ matches the start of the string or line (directly after a \n). $ matches the end of the string or line (directly before a \n). A new line in Windows is nearly always denoted with \r\n rather than just \n. This means that for $ to be useful, you must usually match the \r as well, with a positive lookahead: (?=\r?$) The positive lookahead ensures that \r doesn't become part of the result. The following matches lines that end in ".txt": using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string fileNames = "a.txt" + "\r\n" + "b.doc" + "\r\n" + "c.txt"; string r = @".+\.txt(?=\r?$)"; foreach (Match m in Regex.Matches (fileNames, r, RegexOptions.Multiline)) Console.Write (m + " "); } } The output: a.txt c.txt