Category Archives: Regular Expressions

C#: Regular Expressions to Filter HTML to a Whitelist of Allowable Tags

If you are looking to “sanitize” your HTML to a whitelist of allowable tags, here’s a bit of code that may help. It is a string extension that uses regular expressions to “clean up” your HTML input. The original code … Continue reading

Posted in C#, Extension Methods, Regular Expressions | 1 Comment

Read a CSV file with Regular Expressions in .Net

Here’s how you can read a CSV file using Regular Expressions in .Net: </p> public static DataTable GetDataTableFromCsvFile(string file) { // Where the CSV data goes DataTable dt = new DataTable("CsvData");   // The pattern used to parse the CSV … Continue reading

Posted in C#, Regular Expressions | Leave a comment

Regular Expression (RegEx) to Find Whole Words in a String

Ever want to match and replace whole words with in a string?  Regular expressions (System.Text.RegularExpressions) makes it a one line operation: Regex.Replace(inputText, @"\b" + wordToReplace + @"\b", replacementText, RegexOptions.IgnoreCase); This pattern uses “word boundries” as your delimiters for matching text.

Posted in C#, Regular Expressions | Leave a comment

Regex to Clean Out HTML from Text

Ahh, the power (and sometimes slight confusion) of regular expressions.  This seems to work well to remove HTML from text: string htmlstring = { some chunk o html infested text }; Regex cleanOutHtml =     new Regex(@”

Posted in C#, Regular Expressions | 1 Comment