SQLserver中使用正则表达式

一、新建.net类库项目

  1. 创建类库项目,名为MSSQLRegexExtend
  2. 创建一个类,名为RegexExtend
  3. 复制下面代码到类中
    [csharp] view plain copy
     
    1. using System.Text.RegularExpressions;  
    2.   
    3. namespace MSSQLRegexExtend  
    4. {  
    5.   
    6.     public class RegexExtend  
    7.     {  
    8.         /// <summary>  
    9.         /// 正则匹配  
    10.         /// </summary>  
    11.         /// <param name="regex">正则表达式</param>  
    12.         /// <param name="input">文本</param>  
    13.         /// <returns></returns>  
    14.         [Microsoft.SqlServer.Server.SqlFunction]  
    15.         public static string Match(string regex, string input)  
    16.         {  
    17.             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Value;  
    18.         }  
    19.   
    20.         /// <summary>  
    21.         /// 正则替换  
    22.         /// </summary>  
    23.         /// <param name="regex">正则表达式</param>  
    24.         /// <param name="input">文本</param>  
    25.         /// <param name="replace">要替换的目标</param>  
    26.         /// <returns></returns>  
    27.         [Microsoft.SqlServer.Server.SqlFunction]  
    28.         public static string Replace(string regex, string input, string replace)  
    29.         {  
    30.             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Replace(input, replace);  
    31.         }  
    32.   
    33.         /// <summary>  
    34.         /// 正则校验  
    35.         /// </summary>  
    36.         /// <param name="regex">正则表达式</param>  
    37.         /// <param name="input">文本</param>  
    38.         /// <returns></returns>  
    39.         [Microsoft.SqlServer.Server.SqlFunction]  
    40.         public static bool IsMatch(string regex, string input)  
    41.         {  
    42.             return !string.IsNullOrEmpty(input) && new Regex(regex, RegexOptions.IgnoreCase).IsMatch(input);  
    43.         }  
    44.     }  
    45. }  
  4. 右击项目生成

二、将类库注册到MSSQL中

在数据库中执行如下脚本(类库存放地址得自己修改正确)。

[sql] view plain copy
 
  1. --DROP ASSEMBLY Regex  
  2. CREATE ASSEMBLY Regex from 'E:CSharpMSSQLRegexExtendMSSQLRegexExtendinReleaseMSSQLRegexExtend.dll' WITH PERMISSION_SET = SAFE --注册.net类库  
  3.   
  4. sp_configure 'clr enabled', 1   --将数据库设置为可以使用clr组件  
  5. RECONFIGURE         --设置可用clr组件。别忘记运行这行进行应用  
  6.   
  7. /****以下代码将类库中的静态方法注册为函数****/  
  8.   
  9. /****正则匹配****/  
  10. --DROP FUNCTION [dbo].[Regex.Match]  
  11. CREATE FUNCTION [dbo].[Regex.Match](@Regex [nvarchar](max),@Input [nvarchar](max))  
  12. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER  
  13. AS   
  14. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Match]  
  15.   
  16. /****正则替换****/  
  17. --DROP FUNCTION [dbo].[Regex.Replace]  
  18. CREATE FUNCTION [dbo].[Regex.Replace](@Regex [nvarchar](max),@Input [nvarchar](max),@Replace [nvarchar](max))  
  19. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER  
  20. AS   
  21. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Replace]  
  22.   
  23. /****正则校验****/  
  24. --DROP FUNCTION [dbo].[Regex.IsMatch]  
  25. CREATE FUNCTION [dbo].[Regex.IsMatch](@Regex [nvarchar](max),@Input [nvarchar](max))  
  26. RETURNS [bit] WITH EXECUTE AS CALLER  
  27. AS   
  28. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[IsMatch]  

三、调用示例

[sql] view plain copy
 
  1. SELECT [CustomerID]  
  2.       ,[CompanyName]  
  3.       ,[ContactName]  
  4.       ,[ContactTitle]  
  5.       ,[City]  
  6.       ,[Region]  
  7.       ,[PostalCode]  
  8.       ,[Country]  
  9.       ,[Phone]  
  10.       ,[Fax]  
  11.       ,[Address]  
  12.       ,[dbo].[Regex.Match]('(d)+',[Address]) as [门牌号码]     --正则匹配  
  13.       ,[dbo].[Regex.Replace]('d',[Address],'*') as [将门牌号码打码]   --正则替换  
  14.   FROM [Northwind].[dbo].[Customers]  
  15.   where [dbo].[Regex.IsMatch]('d',[Address])=1             --正则校验有门牌号码的记录  

 

原文地址:https://www.cnblogs.com/mingjing/p/7765568.html