sqlserver 使用正则表达式

原文:https://www.jianshu.com/p/ff16a7da6de0

1、vs添加类库,编写如下代码,生成dll

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Text.RegularExpressions;
 6 using System.Threading.Tasks;
 7 
 8 namespace MSSQLRegexExtend
 9 {
10     public class RegexExtend
11     {
12         /// <summary>
13         /// 正则匹配
14         /// </summary>
15         /// <param name="regex">正则表达式</param>
16         /// <param name="input">文本</param>
17         /// <returns></returns>
18         [Microsoft.SqlServer.Server.SqlFunction]
19         public static string Match(string regex, string input)
20         {
21             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Value;
22         }
23 
24         /// <summary>
25         /// 正则替换
26         /// </summary>
27         /// <param name="regex">正则表达式</param>
28         /// <param name="input">文本</param>
29         /// <param name="replace">要替换的目标</param>
30         /// <returns></returns>
31         [Microsoft.SqlServer.Server.SqlFunction]
32         public static string Replace(string regex, string input, string replace)
33         {
34             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Replace(input, replace);
35         }
36 
37         /// <summary>
38         /// 正则校验
39         /// </summary>
40         /// <param name="regex">正则表达式</param>
41         /// <param name="input">文本</param>
42         /// <returns></returns>
43         [Microsoft.SqlServer.Server.SqlFunction]
44         public static bool IsMatch(string regex, string input)
45         {
46             return !string.IsNullOrEmpty(input) && new Regex(regex, RegexOptions.IgnoreCase).IsMatch(input);
47         }
48     }
49 }
View Code

2、sqlserver中注册此dll

 1 /****注册.net类库****/
 2 use Northwind
 3 --1.注册.net类库
 4 CREATE ASSEMBLY Regex from 'dll的全路径' WITH PERMISSION_SET = SAFE 
 5 
 6 --2.将数据库设置为可以使用clr组件
 7 sp_configure 'clr enabled', 1   
 8 
 9 --3.设置可用clr组件。别忘记运行这行进行应用
10 RECONFIGURE 
11         
12 --4.删除该类库(删除时执行)
13 --DROP ASSEMBLY Regex

3、将dll中的方法注册为sqlserver中的函数

 1 /****以下代码将类库中的静态方法注册为函数****/
 2 --1.正则匹配
 3 CREATE FUNCTION [dbo].[Regex.Match](@Regex [nvarchar](max),@Input [nvarchar](max))
 4 RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
 5 AS 
 6 EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Match]
 7 
 8 --DROP FUNCTION [dbo].[Regex.Match]
 9 
10 --2.正则替换
11 CREATE FUNCTION [dbo].[Regex.Replace](@Regex [nvarchar](max),@Input [nvarchar](max),@Replace [nvarchar](max))
12 RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
13 AS 
14 EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Replace]
15 
16 --DROP FUNCTION [dbo].[Regex.Replace] 
17 
18 --3.正则校验
19 CREATE FUNCTION [dbo].[Regex.IsMatch](@Regex [nvarchar](max),@Input [nvarchar](max))
20 RETURNS [bit] WITH EXECUTE AS CALLER
21 AS 
22 EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[IsMatch]

此时就可以正常调用了

原文地址:https://www.cnblogs.com/xiao-sheng/p/14376531.html