SQLserver中使用正則表達式
阿新 • • 發佈:2017-11-01
tro max dll count 組件 too 可用 mat comm
一、新建.net類庫項目
- 創建類庫項目,名為MSSQLRegexExtend
- 創建一個類,名為RegexExtend
- 復制下面代碼到類中
[csharp] view plain copy
- using System.Text.RegularExpressions;
- namespace MSSQLRegexExtend
- {
- public class RegexExtend
- {
- /// <summary>
- /// 正則匹配
- /// </summary>
- /// <param name="regex">正則表達式</param>
- /// <param name="input">文本</param>
- /// <returns></returns>
- [Microsoft.SqlServer.Server.SqlFunction]
- public static string Match(string regex, string input)
- {
- return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Value;
- }
- /// <summary>
- /// 正則替換
- /// </summary>
- /// <param name="regex">正則表達式</param>
- /// <param name="input">文本</param>
- /// <param name="replace">要替換的目標</param>
- /// <returns></returns>
- [Microsoft.SqlServer.Server.SqlFunction]
- public static string Replace(string regex, string input, string replace)
- {
- return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Replace(input, replace);
- }
- /// <summary>
- /// 正則校驗
- /// </summary>
- /// <param name="regex">正則表達式</param>
- /// <param name="input">文本</param>
- /// <returns></returns>
- [Microsoft.SqlServer.Server.SqlFunction]
- public static bool IsMatch(string regex, string input)
- {
- return !string.IsNullOrEmpty(input) && new Regex(regex, RegexOptions.IgnoreCase).IsMatch(input);
- }
- }
- }
- 右擊項目生成
二、將類庫註冊到MSSQL中
在數據庫中執行如下腳本(類庫存放地址得自己修改正確)。
[sql] view plain copy- --DROP ASSEMBLY Regex
- CREATE ASSEMBLY Regex from ‘E:\CSharp\MSSQLRegexExtend\MSSQLRegexExtend\bin\Release\MSSQLRegexExtend.dll‘ WITH PERMISSION_SET = SAFE --註冊.net類庫
- sp_configure ‘clr enabled‘, 1 --將數據庫設置為可以使用clr組件
- RECONFIGURE --設置可用clr組件。別忘記運行這行進行應用
- /****以下代碼將類庫中的靜態方法註冊為函數****/
- /****正則匹配****/
- --DROP FUNCTION [dbo].[Regex.Match]
- CREATE FUNCTION [dbo].[Regex.Match](@Regex [nvarchar](max),@Input [nvarchar](max))
- RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
- AS
- EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Match]
- /****正則替換****/
- --DROP FUNCTION [dbo].[Regex.Replace]
- CREATE FUNCTION [dbo].[Regex.Replace](@Regex [nvarchar](max),@Input [nvarchar](max),@Replace [nvarchar](max))
- RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
- AS
- EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Replace]
- /****正則校驗****/
- --DROP FUNCTION [dbo].[Regex.IsMatch]
- CREATE FUNCTION [dbo].[Regex.IsMatch](@Regex [nvarchar](max),@Input [nvarchar](max))
- RETURNS [bit] WITH EXECUTE AS CALLER
- AS
- EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[IsMatch]
三、調用示例
[sql] view plain copy- SELECT [CustomerID]
- ,[CompanyName]
- ,[ContactName]
- ,[ContactTitle]
- ,[City]
- ,[Region]
- ,[PostalCode]
- ,[Country]
- ,[Phone]
- ,[Fax]
- ,[Address]
- ,[dbo].[Regex.Match](‘(\d)+‘,[Address]) as [門牌號碼] --正則匹配
- ,[dbo].[Regex.Replace](‘\d‘,[Address],‘*‘) as [將門牌號碼打碼] --正則替換
- FROM [Northwind].[dbo].[Customers]
- where [dbo].[Regex.IsMatch](‘\d‘,[Address])=1 --正則校驗有門牌號碼的記錄
SQLserver中使用正則表達式