<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code.commongroove.com &#187; Regular Expressions</title>
	<atom:link href="http://code.commongroove.com/category/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.commongroove.com</link>
	<description>C#, T-SQL, and general IT mojo</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:00:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Read a CSV file with Regular Expressions in .Net</title>
		<link>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/</link>
		<comments>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:51:17 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/06/23/read-a-csv-file-with-regular-expressions-in-.net.aspx</guid>
		<description><![CDATA[Here’s how you can read a CSV file using Regular Expressions in .Net: &#60;/p&#62; public static DataTable GetDataTableFromCsvFile&#40;string file&#41; &#123; // Where the CSV data goes DataTable dt = new DataTable&#40;&#34;CsvData&#34;&#41;; // The pattern used to parse the CSV const &#8230; <a href="http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;">Here’s how you can read a CSV file using Regular Expressions in .Net:</span></p>
<p><span style="line-height: 115%;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;">
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable GetDataTableFromCsvFile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> file<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// Where the CSV data goes</span><br />
DataTable dt <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;CsvData&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// The pattern used to parse the CSV</span><br />
<span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> csvPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;,(?=(?:[^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>[^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>)*(?![^<span style="color: #008080; font-weight: bold;">\&quot;</span>]*<span style="color: #008080; font-weight: bold;">\&quot;</span>))&quot;</span><span style="color: #008000;">;</span><br />
Regex csvRegex <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Regex<span style="color: #008000;">&#40;</span>csvPattern<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Read all lines in the file</span><br />
<span style="color: #008080; font-style: italic;">// (not great for large files)</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> fileLines <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllLines</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Get the column headers</span><br />
<span style="color: #008080; font-style: italic;">// (assumes first row has headers and that</span><br />
<span style="color: #008080; font-style: italic;">//  each column contains string values and</span><br />
<span style="color: #008080; font-style: italic;">//  that each column name is unique)</span><br />
<br />
Dictionary<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>int, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> headers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>int, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> headerValues <span style="color: #008000;">=</span> csvRegex<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span>fileLines<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> headerValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
headers<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i, headerValues<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
dt<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> DataColumn<span style="color: #008000;">&#40;</span>headerValues<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>, <span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Then add the the rest of the lines</span><br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> k <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> k <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> fileLines<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> k<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<br />
DataRow dr <span style="color: #008000;">=</span> dt<span style="color: #008000;">.</span><span style="color: #0000FF;">NewRow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #6666cc; font-weight: bold;">string</span> line <span style="color: #008000;">=</span> fileLines<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> cols <span style="color: #008000;">=</span> csvRegex<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span>line<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> cols<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> header <span style="color: #008000;">=</span> headers<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> cols<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// remove quotes around the field</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
dr<span style="color: #008000;">&#91;</span>header<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
dt<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>dr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">return</span> dt<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Of course, you will need to add in error catching and handling as well.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/06/23/read-a-csv-file-with-regular-expressions-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression (RegEx) to Find Whole Words in a String</title>
		<link>http://code.commongroove.com/2008/07/31/regular-expression-regex-to-find-words-in-a-string/</link>
		<comments>http://code.commongroove.com/2008/07/31/regular-expression-regex-to-find-words-in-a-string/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 05:07:58 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2008/07/30/regular-expression-regex-to-find-words-in-a-string.aspx</guid>
		<description><![CDATA[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&#40;inputText, @&#34;\b&#34; + wordToReplace + @&#34;\b&#34;, replacementText, RegexOptions.IgnoreCase&#41;; This pattern uses &#8220;word boundries&#8221; as your delimiters for matching text.]]></description>
			<content:encoded><![CDATA[<p>Ever want to match and replace whole words with in a string?  Regular expressions (System.Text.RegularExpressions) makes it a one line operation:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>inputText, <span style="color: #666666;">@&quot;\b&quot;</span> <span style="color: #008000;">+</span> wordToReplace <span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;\b&quot;</span>, replacementText, RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This pattern uses &#8220;word boundries&#8221; as your delimiters for matching text.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2008/07/31/regular-expression-regex-to-find-words-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regex to Clean Out HTML from Text</title>
		<link>http://code.commongroove.com/2007/10/18/regex-to-clean-out-html-from-text/</link>
		<comments>http://code.commongroove.com/2007/10/18/regex-to-clean-out-html-from-text/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 00:14:41 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2008/07/30/regex-to-clean-out-html-from-text.aspx</guid>
		<description><![CDATA[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(@&#8221;]]></description>
			<content:encoded><![CDATA[<p>Ahh, the power (and sometimes slight confusion) of regular expressions.  This seems to work well to remove HTML from text:</p>
<p><span style="font-family: Courier New;"><span style="color: rgb(0, 0, 255);">string </span>htmlstring = { some chunk o html infested text };</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;"><span style="color: rgb(51, 153, 102);">Regex </span>cleanOutHtml = <br />
    <span style="color: rgb(0, 0, 255);">new </span><span style="color: rgb(51, 153, 102);">Regex</span>(<span style="color: rgb(153, 51, 0);">@&#8221;</?\w+((\s+\w+(\s*=\s*(?:""(.|\n)*?""|'(.|\n)*?'|[^'"">\s]+))?)+\s*|\s*)/?>&#8221;</span>);</span><br style="font-family: Courier New;" /><br />
<span style="font-family: Courier New;"><span style="color: rgb(0, 0, 255);">string </span>onlytext = cleanOutHtml.Replace(htmlstring, <span style="color: rgb(128, 0, 0);">&#8220;&#8221;</span>);</span></p>
<p>Weee there you have it.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2007/10/18/regex-to-clean-out-html-from-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

