如何在SQL Server中使用正则表达式?

yog*_*239 6 regex sql sql-server sql-server-2008 sql-server-ce

是否有可能进行使用正则表达式功能集的高效查询.我的表格中的数据格式不正确,EX:-In Title colum:Cable180â"¬-"To90â"â€"串行ATA Cable和Id列123234+数据采用指数格式,可以在Sqlserver2008中使用正则表达式进行查询.

HOT*_*nKk 7

您需要使用以下内容.通常需要三者的组合:

  1. patindex
  2. charindex
  3. substring

在回复上面的评论时,patindex不应该在找到案例的情况下为0.patindex找到指定模式的起始位置,因此如果patindex找到大小写,则应返回大于0的整数.

编辑:

此外,len(string)reverse(string)在特定场合派上用场.


Dar*_*nMB 5

将CLR和.NET项目发布到SQL Server后,它非常高效.在过去的两年里,我开始在VB.Net中使用我们的2005 SQL Server中的CLR项目后,我发现TSQL中的每个标量函数都已经用.NET版本替换,它大大提高了性能时间.我用它进行高级日期操作,格式化和解析,字符串格式化和解析,MD5哈希生成,矢量长度,字符串JOIN Aggragate功能,分割表值功能,甚至通过共享文件夹从序列化数据表批量加载(这是惊人的快速).

对于RegEx,因为它还没有出现,我只能假设它与编译的EXE一样有效,它会做同样的REGEX,也就是说非常快.

我将从我的VB.Net CLR项目共享一个代码文件,该文件允许一些RegEx功能.此代码将是发布到您的服务器的.NET CLR DLL的一部分.

功能摘要

Regex_IsMatch(Data,Parttern,Options)AS tinyint(0/1结果)

例如.SELECT dbo.Regex_IsMatch('Darren','[trwq] en $',NULL) - 返回1/true

Regex_Group(数据,模式,组名,选项)为nvarchar(max)(返回捕获组值)

例如.SELECT dbo.Regex_Group('Cable 180 + e10 to 120 + e3','(?[0-9] +)+ e [0-9] +','n',NULL) - 返回'180'

Regex_Replace(数据,模式,替换,选项)为nvarchar(max)(返回修改后的字符串)

例如.SELECT dbo.Regex_Replace('Cable 180 + e10 to 120 + e3','(?[0-9] +)+ e(?[0-9] +)','$ {e}:$ {n]' ,NULL) - 返回'电缆10:180到3:120'

Partial Public Class UserDefinedFunctions

    ''' <summary>
    ''' Returns 1 (true) or 0 (false) if a pattern passed is matched in the data passed.
    ''' Returns NULL if Data is NULL.
    ''' options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="pattern"></param>
    ''' <param name="options">options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Microsoft.SqlServer.Server.SqlFunction()> _
    Public Shared Function Regex_IsMatch(data As SqlChars, pattern As SqlChars, options As SqlString) As SqlByte
        If pattern.IsNull Then
            Throw New Exception("Pattern Parameter in ""RegEx_IsMatch"" cannot be NULL")
        End If
        If data.IsNull Then
            Return SqlByte.Null
        Else
            Return CByte(If(Regex.IsMatch(data.Value, pattern.Value, Regex_Options(options)), 1, 0))
        End If
    End Function

    ''' <summary>
    ''' Returns the Value of a RegularExpression Pattern Group by Name or Number.
    ''' Group needs to be captured explicitly. Example Pattern "[a-z](?&lt;m&gt;[0-9][0-9][0-9][0-9])" to capture the numeric portion of an engeneering number by the group called "m".
    ''' Returns NULL if The Capture was not successful.
    ''' Returns NULL if Data is NULL.
    ''' options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="pattern"></param>
    ''' <param name="groupName">Name used in the explicit capture group</param>
    ''' <param name="options">options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"</param>
    <Microsoft.SqlServer.Server.SqlFunction()> _
    Public Shared Function Regex_Group(data As SqlChars, pattern As SqlChars, groupName As SqlString, options As SqlString) As SqlChars
        If pattern.IsNull Then
            Throw New Exception("Pattern Parameter in ""RegEx_IsMatch"" cannot be NULL")
        End If
        If groupName.IsNull Then
            Throw New Exception("GroupName Parameter in ""RegEx_IsMatch"" cannot be NULL")
        End If
        If data.IsNull Then
            Return SqlChars.Null
        Else
            Dim m As Match = Regex.Match(data.Value, pattern.Value, Regex_Options(options))
            If m.Success Then
                Dim g As Group
                If IsNumeric(groupName.Value) Then
                    g = m.Groups(CInt(groupName.Value))
                Else
                    g = m.Groups(groupName.Value)
                End If
                If g.Success Then
                    Return New SqlChars(g.Value)
                Else ' group did not return or was not found.
                    Return SqlChars.Null
                End If
            Else 'match failed.
                Return SqlChars.Null
            End If
        End If
    End Function

    ''' <summary>
    ''' Does the Equivalent toi Regex.Replace in .NET.
    ''' Replacement String Replacement Markers are done in this format "${test}" = Replaces the capturing group (?&lt;test&gt;...)
    ''' If the replacement pattern is $1 or $2 then it replaces the first or second captured group by position.
    ''' Returns NULL if Data is NULL.
    ''' options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="pattern"></param>
    ''' <param name="replacement">Replacement String Replacement Markers are done in this format "${test}" = Replaces the capturing group (?&lt;test&gt;...). If the replacement pattern is $1 or $2 then it replaces the first or second captured group by position.</param>
    ''' <param name="options">options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <SqlFunction()> _
    Public Shared Function Regex_Replace(data As SqlChars, pattern As SqlChars, replacement As SqlChars, options As SqlString) As SqlChars
        If pattern.IsNull Then
            Throw New Exception("Pattern Parameter in ""Regex_Replace"" cannot be NULL")
        End If
        If replacement.IsNull Then
            Throw New Exception("Replacement Parameter in ""Regex_Replace"" cannot be NULL")
        End If
        If data.IsNull Then
            Return SqlChars.Null
        Else
            Return New SqlChars(Regex.Replace(data.Value, pattern.Value, replacement.Value, Regex_Options(options)))
        End If
    End Function

    ''' <summary>
    ''' Buffered list of options by name for speed.
    ''' </summary>
    Private Shared m_Regex_Buffered_Options As New Generic.Dictionary(Of String, RegexOptions)(StrComp)
    ''' <summary>
    ''' Default regex options used when options value is NULL or an Empty String
    ''' </summary>
    Private Shared ReadOnly m_Regex_DefaultOptions As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture Or RegexOptions.Multiline

    ''' <summary>
    ''' Get the regular expressions options to use by a passed string of data.
    ''' Formatted like command line arguments.
    ''' </summary>
    ''' <param name="options">options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline "</param>
    Private Shared Function Regex_Options(options As SqlString) As RegexOptions
        Return Regex_Options(If(options.IsNull, "", options.Value))
    End Function

    ''' <summary>
    ''' Get the regular expressions options to use by a passed string of data.
    ''' Formatted like command line arguments.
    ''' </summary>
    ''' <param name="options">options example, full or partial names can be used after slashes or hypens with or without spaces, some are exclusive of each other "/ic /ex -s" = "\ignorecase -explicitcapture/singleline"</param>
    Private Shared Function Regex_Options(options As String) As RegexOptions
        'empty options string is considered default options.
        If options Is Nothing OrElse options = "" Then
            Return m_Regex_DefaultOptions
        Else
            Dim out As RegexOptions
            If m_Regex_Buffered_Options.TryGetValue(options, out) Then
                Return out
            Else
                'must build options and store them
                If options Like "*[/\-]n*" Then
                    out = RegexOptions.None
                End If
                If options Like "*[/\-]s*" Then
                    out = out Or RegexOptions.Singleline
                End If
                If options Like "*[/\-]m*" Then
                    out = out Or RegexOptions.Multiline
                End If
                If options Like "*[/\-]co*" Then
                    out = out Or RegexOptions.Compiled
                End If
                If options Like "*[/\-]c[ui]*" Then
                    out = out Or RegexOptions.CultureInvariant
                End If
                If options Like "*[/\-]ecma*" Then
                    out = out Or RegexOptions.ECMAScript
                End If
                If options Like "*[/\-]e[xc]*" Then
                    out = out Or RegexOptions.ExplicitCapture
                End If
                If options Like "*[/\-]i[c]*" OrElse options Like "*[/\-]ignorec*" Then
                    out = out Or RegexOptions.IgnoreCase
                End If
                If options Like "*[/\-]i[pw]*" OrElse options Like "*[/\-]ignore[pw]*" Then
                    out = out Or RegexOptions.IgnorePatternWhitespace
                End If
                If options Like "*[/\-]r[tl]*" Then
                    out = out Or RegexOptions.RightToLeft
                End If
                'store the options for next call (for speed)
                m_Regex_Buffered_Options(options) = out
                Return out
            End If
        End If
    End Function

End Class
Run Code Online (Sandbox Code Playgroud)