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 }
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]
此时就可以正常调用了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)