日期:2014-05-18 浏览次数:20647 次
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports Microsoft.SqlServer.Server Imports System.Text.RegularExpressions Partial Public Class StoredProcedures <SqlFunction()> _ Public Shared Function regexes(ByVal expression As SqlString) As SqlString Dim regex As Regex = New Regex(" ") ' Split on hyphens. 'Dim substrings() As String = regex.Split("plum * pear * BALL") Dim substrings() As String = regex.Split(expression) Dim c As String = vbNullString For Each match As String In substrings If match = "*" Then '匹配有通配符才转换 match = ".*" End If c += match & " " Next Dim c1 As String = c.Trim(" ") 'Trim截去前后不必要的 Dim regex1 As String = c1 Return regex1 '返回值相当于 regexes = pattern End Function <SqlFunction()> _ Public Shared Function regexismatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlBoolean If input.IsNull OrElse pattern.IsNull Then Return False End If Return Regex.IsMatch(input.Value, pattern.Value) End Function <SqlFunction()> _ Public Shared Function regexmatch(ByVal input As SqlString, ByVal pattern As SqlString) As SqlString If input.IsNull OrElse pattern.IsNull Then End If Dim inputx As String = input Dim patternx As String = pattern Dim m As Match = Regex.Match(inputx, patternx) If m.Success Then Return input Else Return False End If End Function End Class
--我本地没有vs环境,我用两个函数先代替一下,就是个思路问题。 /* create function dbo.regexes (@p varchar(4)) returns varchar(10) as begin return @p+'a' end go create function dbo.regexmatch(@a varchar(10),@b varchar(10)) returns varchar(10) as begin return @a+@b end */ --上面两个函数就是两个函数,没有实际意义,就是为了我本地能够正常运行。 --假设这是你的aiml2的数据 declare @aiml2 table (pattern varchar(2)) insert into @aiml2 select 'a1' union all select 'b1' union all select 'c1' union all select 'd1' --你现在的实现方式 declare @regex varchar(max) select @regex = dbo.regexes(pattern) from @aiml2 select @regex declare @input varchar(max) set @input='ok ONES' select dbo.regexmatch(@input,@regex) --你现在的结果,只处理了一行 /* ok ONESd1a */ --应该是这样的,不用@t也是可以的。 declare @input1 varchar(max) set @input1='ok ONES' select dbo.regexmatch(@input1,dbo.regexes(pattern)) from @aiml2 /* ok ONESa1a ok ONESb1a ok ONESc1a ok ONESd1a */