Compare Page Revisions
« Older Revision - Back to Page History - Current Revision
dbo.RegExSearch
select ZipCode = dbo.RegExSearch( 'My ZIP code is 30303, which is in Atlanta, Georgia.' ,'(?<zip>[0-9]{5})' ,'zip' )
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports Microsoft.SqlServer.Server Imports System.Text.RegularExpressions Partial Public Class UserDefinedFunctions <Microsoft.SqlServer.Server.SqlFunction()> _ Public Shared Function RegExSearch(ByVal searchIn As SqlString, ByVal pattern As SqlString, _ ByVal groupName As SqlString) As SqlString Dim result As String = "" Dim regex As Regex = New Regex(pattern.Value) Dim mc As MatchCollection = regex.Matches(searchIn.Value) If mc IsNot Nothing _ AndAlso mc.Count > 0 _ AndAlso mc(0).Groups.Count > 0 _ AndAlso mc(0).Groups(groupName.Value) IsNot Nothing Then result = mc(0).Groups(groupName.Value).Value End If Return New SqlString(result) End Function End Class
TODO
<Microsoft.SqlServer.Server.SqlFunction()> _ Public Shared Function ParseMyText(ByVal input As SqlString) As SqlDouble Dim inputValue As String = input.Value Dim pattern As String = "(?<descrip>.+?)[$](?<value>[0-9]{1,10}(.[0-9]{1,10}){0,1})" Dim regex As Regex = New Regex(pattern) Dim mc As MatchCollection = regex.Matches(inputValue) Dim result As Double = 0 If mc IsNot Nothing AndAlso mc.Count > 0 Then For Each m As Match In mc Dim desc As String = m.Groups("descrip").Value.Trim() If Not desc.Contains("some text") Then Dim v As String = m.Groups("value").Value Dim d As Double = 0 If Double.TryParse(v, d) Then result += d End If End If Next End If Return New SqlDouble(result) End Function
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.