Regular Expressions
Things to do with regular expressions
Here’s how you can read a CSV file using Regular Expressions in .Net:
public static DataTable GetDataTableFromCsvFile(string file)
{
// Where the CSV data goes
DataTable dt = new DataTable("CsvData");
// The pattern used to parse the CSV
const string csvPattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
Regex csvRegex = new Regex(csvPattern);
// Read all lines in the file
// (not great for large files)
string[] fileLines = File.ReadAllLines(file);
// Get the column headers
// (assumes first row has headers and that
// each column contains string values and
// that each column name is unique)
Dictionary<int, string> headers = new Dictionary<int, 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.
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(@"</?\w+((\s+\w+(\s*=\s*(?:""(.|\n)*?""|'(.|\n)*?'|[^'"">\s]+))?)+\s*|\s*)/?>");
string onlytext = cleanOutHtml.Replace(htmlstring, "");
Weee there you have it.