gpt4 book ai didi

T-SQL中使用正则表达式函数

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章T-SQL中使用正则表达式函数由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

首先,我们在VSTS中创建一Database Project,增一个class, 实现下面的一个方法:  。

复制代码代码如下

/// <summary>  /// Regs the ex match.  /// </summary>  /// <param name="inputValue">The input value.</param>  /// <param name="regexPattern">The regex pattern.</param>  /// <remarks>Author: Petter Liu http://wintersun.cnblogs.com </remarks>  /// <returns>1 match,0 not match</returns>  [SqlFunction]  public static bool RegExMatch(string inputValue, string regexPattern)  {  // Any nulls - we can't match, return false  if (string.IsNullOrEmpty(inputValue) || string.IsNullOrEmpty(regexPattern))  return false;  Regex r1 = new Regex(regexPattern.TrimEnd(null));  return r1.Match(inputValue.TrimEnd(null)).Success;  }  。

好了,Build后Deploy到你的Target database就OK了,VisualStudio会自动注册这个程序集的。如果,你想手动注册程序集,可执行以下的T-SQL:  。

复制代码代码如下

CREATE ASSEMBLY [RegExCLR] FROM 'RegExCLR.dll';  -- 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的正则表达式。  完了,希望这篇POST对您有帮助.

最后此篇关于T-SQL中使用正则表达式函数的文章就讲到这里了,如果你想了解更多关于T-SQL中使用正则表达式函数的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com