<?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>DataBatrix &#187; Regular Expressions</title>
	<atom:link href="http://www.databatrix.com/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.databatrix.com</link>
	<description>The workings of Eric Bridges</description>
	<lastBuildDate>Thu, 01 Dec 2011 23:47:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Custom Replace with Regular Expressions in .NET</title>
		<link>http://www.databatrix.com/2009/08/custom-replace-with-regular-expressions-in-net/</link>
		<comments>http://www.databatrix.com/2009/08/custom-replace-with-regular-expressions-in-net/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 19:05:34 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Delegates]]></category>

		<guid isPermaLink="false">http://www.databatrix.com/?p=149</guid>
		<description><![CDATA[This is the second time I have had to do something like this.  So this time I am posting it for my reference.  Regular expressions are amazing and amazingly difficult to understand sometimes, but they come in very handy.  I was in a situation where I was going to need to  feed in a custom [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second time I have had to do something like this.  So this time I am posting it for my reference.  Regular expressions are amazing and amazingly difficult to understand sometimes, but they come in very handy.  I was in a situation where I was going to need to  feed in a custom template to be used similar to a &#8220;mail merge&#8221; situation.  I would query a database, then for each record I would fill out an email using an email template.  I decided to go with &lt;:FieldName:&gt; as the nomenclature of how a field would be inserted.  For example, here are the query results:</p>
<table border="0">
<tbody>
<tr>
<td>FirstName</td>
<td>LastName</td>
<td>Company</td>
</tr>
<tr>
<td>Eric</td>
<td>Bridges</td>
<td>DataBatrix</td>
</tr>
</tbody>
</table>
<p>My email template may look something like this:</p>
<pre>Dear &lt;:FirstName:&gt; &lt;:LastName:&gt;,</pre>
<pre>You work for &lt;:Company:&gt;!</pre>
<p>For the purpose of my project, I did not know what fields were going to be returned or what the email template looked like.  I needed a simple way to replace all of the field placeholders with actual data.  Regular expressions to the rescue!  You can use a delegate to run your own replace function.  I called my method MatchHandler which is passed a Match variable (the match that is found).  In my example below, I replaced it with the corresponding item from _reader (my sqldatareader row).</p>
<pre class="brush: vb">    Private Function MatchHandler(ByVal m As Match) As String
        Dim fieldName As String = m.Groups("fieldName").ToString()
        If HasField(fieldName) Then
            Return _reader(fieldName)
        Else
            Return m.ToString()
        End If
    End Function</pre>
<p>Next we have to create our delegate.  Delegates are basically pointers for methods (functions/subs).  I called my delegate &#8220;MatchDelegate&#8221; and pass it the address of my function I created above.</p>
<pre class="brush: vb">    Dim MatchDelegate As New MatchEvaluator(AddressOf MatchHandler)</pre>
<p>Now to make it work!  Define your regular expression pattern and instantiate a new RegularExpression object.  Use the Replace method and pass the string to search and the delegate that we created.</p>
<pre class="brush: vb">    Private Function MergeFields(ByVal body As String) As String
        Dim returnValue As String = ""
        Dim pattern As String = "&lt;:(?&lt;fieldName:&gt;(.+?)):&gt;"
        Dim regex As New Text.RegularExpressions.Regex(pattern)
        returnValue = regex.Replace(body, MatchDelegate)
        Return returnValue
    End Function</pre>
<p>That&#8217;s it!  Now you can do whatever you like when a match is found in your Replace method.</p>
<p>In case you were wondering what &#8220;(?&lt;fieldName:&gt;(.+?))&#8221; in the pattern was all about, check out <a href="http://www.regular-expressions.info/named.html" target="_blank">this link talking about regular expression groups</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databatrix.com/2009/08/custom-replace-with-regular-expressions-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

