-- Add the REGEX function. We want a friendly name -- RegExMatch rather than the full namespace name. -- Note the way we have to specify the Assembly.Namespace.Class.Function -- NOTE the RegExCLR.RegExCLR -- (one is the assembly the other is the namespace) CREATE FUNCTION RegExMatch ( @inputCalue NVARCHAR(4000), @regexPattern NVARCHAR(4000) ) RETURNS BIT AS EXTERNAL NAME RegExCLR.RegExCLR.ClrClass.RegExMatch;
OK, 一切OK的后,我们来测试下:
select COUNT(1) from Threads where dbo.RegExMatch(ThreadId,'^[{|/(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[/)|}]?$')=1 上面的T-SQL是找出Threads表ThreadId是GUID的记录数。 等于1是匹配,^[{|/(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[/)|}]?$ 匹配GUID的正则表达式。