Regular expressions can be a great addition to the Find and Replace functionality within Visual Studio, specially when you want do do some transformation on the text you are looking up. Lately I’m working on a simple conversion class to transform some HTML into RTF. I got a list of special characters in HTML that I had to convert to the RTF equivalent and for that I needed to create a dictionary. The values where listed in a text document like this:
\'b0 \tab °\par
\'b1 \tab ±\par
\'b2 \tab ²\par
\'b3 \tab ³\par
\'b4 \tab ´\par
\'b5 \tab µ\par
\'b6 \tab ¶\par
\'b7 \tab ·\par
\'b8 \tab ¸\par
\'b9 \tab ¹\par
\'ba \tab º\par
\'bb \tab »\par
\'bc \tab ¼\par
\'bd \tab ½\par
\'be \tab ¾\par
And I needed to create a dictionary with the key being the HTML code (third column) and the RTF code (first column) the value. Like this:
Dictionary<string, string> charsMapping =
new Dictionary<string, string>();
charsMapping.Add("€", @"\'80");
Here is the expression to find the matches within Visual Studio: __ {\\’:a:a}{.}{&:a;}{\\par}
Each pair of { } is a group to be matched. The difference is that the usual \w (letters or numbers) is :a. In my expression I have four groups the fist is the match for the RTF character and the third is the match for the HTML code. Knowing that the Replace expression is as follows: __charsMapping.Add(”\3”, @”\1”);
Here is how it would look like in VS:

Doing this is much easier then creating the dictionary by hand of even writing a console app to do the replace. Sure I could have used some other text tool to do the same but it wouldn’t be as much fun as figuring out how to do it in VS.
Here is the final output:
Dictionary<string, string> charsMapping =
new Dictionary<string, string>();
charsMapping.Add("°", @"\'b0");
charsMapping.Add("±", @"\'b1");
charsMapping.Add("²", @"\'b2");
charsMapping.Add("³", @"\'b3");
charsMapping.Add("´", @"\'b4");
charsMapping.Add("µ", @"\'b5");
charsMapping.Add("¶", @"\'b6");
charsMapping.Add("·", @"\'b7");
charsMapping.Add("¸", @"\'b8");
charsMapping.Add("¹", @"\'b9");
charsMapping.Add("º", @"\'ba");
charsMapping.Add("»", @"\'bb");
charsMapping.Add("¼", @"\'bc");
charsMapping.Add("½", @"\'bd");
charsMapping.Add("¾", @"\'be");
charsMapping.Add("¿", @"\'bf");
Of course the number of characters was much bigger but it doesn’t matter here. If you need more info on regular expressions you might want to check out the Regular-Expressions.info site, they have a lot of good info on the subject. There are some specifics to using regular expressions on VS so you need to make sure to visit MSDN Reference.