Tip: Regex.Escape
A few days ago I received a patch for BlogEngine.NET through CodePlex. In that patch I saw a little piece of code I’ve never seen before, namely Regex.Escape(string). Now, this is nothing new in the .NET framework, I just never come across it before.
It takes care of a trivial issue I have from time to time when dealing with regular expressions. If you pass in a string into a regular expression, you need to make sure some special characters are escaped first. It could be characters such as #, *, ? and $. Instead of replacing all the characters manually you can use the static Escape method instead like so:
string escapedText = Regex.Escape("Some text with special characters #?+^");
Just thought I’d share in case you also missed this method.
Comments
Comments are closed