SQL Server T-SQL中的正则expression式

SQL Server中是否有用T-SQL (没有CLR,没有扩展sp,纯t-sql)编写的正则expression式库?

(应与共享主机一起工作)

编辑:

  • 感谢我了解PATINDEX,LIKE,xp_sps和CLR解决scheme

  • 我也知道这不是正则expression式的最佳地方,问题是理论:)

  • 减less的function也被接受

PATINDEX函数怎么样?

TSQL中的模式匹配不是一个完整的正则expression式库,但它给你的基本知识。

(来自在线图书)

 Wildcard Meaning % Any string of zero or more characters. _ Any single character. [ ] Any single character within the specified range (for example, [af]) or set (for example, [abcdef]). [^] Any single character not within the specified range (for example, [^a - f]) or set (for example, [^abcdef]). 

有一些基本的模式匹配可以通过使用LIKE,其中%匹配任何数字和字符组合,_匹配任何一个字符,并且[abc]可以匹配a,b或c … MSDN站点上有更多信息。

如果有人有兴趣在CLR中使用正则expression式,这是一个解决scheme。 下面的函数(C#.net 4.5)在模式匹配的情况下返回1,如果模式不匹配则返回0。 我用它来标记子查询中的行。 SQLfunction属性告诉SQL服务器这个方法是SQL服务器将使用的实际的UDF。 将文件保存为一个dll,您可以从management studio访问它。

 // default using statements above using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Text.RegularExpressions; namespace CLR_Functions { public class myFunctions { [SqlFunction] public static SqlInt16 RegexContain(SqlString text, SqlString pattern) { SqlInt16 returnVal = 0; try { string myText = text.ToString(); string myPattern = pattern.ToString(); MatchCollection mc = Regex.Matches(myText, myPattern); if (mc.Count > 0) { returnVal = 1; } } catch { returnVal = 0; } return returnVal; } } } 

在pipe理工作室导入dll文件通过可编程序 – 程序集 – 新的程序集

然后运行这个查询:

 CREATE FUNCTION RegexContain(@text NVARCHAR(50), @pattern NVARCHAR(50)) RETURNS smallint AS EXTERNAL NAME CLR_Functions.[CLR_Functions.myFunctions].RegexContain 

然后,您应该可以通过stored procedures集的数据库完全访问该function。

然后用于像这样的查询:

 SELECT * FROM ( SELECT DailyLog.Date, DailyLog.Researcher, DailyLog.team, DailyLog.field, DailyLog.EntityID, DailyLog.[From], DailyLog.[To], dbo.RegexContain(Researcher, '[\p{L}\s]+') as 'is null values' FROM [DailyOps].[dbo].[DailyLog] ) AS a WHERE a.[is null values] = 0 

如果其他人仍然在看这个问题, http://www.sqlsharp.com/是一个免费,; 简单的方法来将正则expression式CLR函数添加到您的数据库。

您可以使用OLE自动化使用VBScript正则expression式function。 这比创build和维护程序集的开销要好得多。 请确保您阅读注释部分以获得更好的主版本的修改版本。

http://blogs.msdn.com/b/khen1234/archive/2005/05/11/416392.aspx

 DECLARE @obj INT, @res INT, @match BIT; DECLARE @pattern varchar(255) = '<your regex pattern goes here>'; DECLARE @matchstring varchar(8000) = '<string to search goes here>'; SET @match = 0; -- Create a VB script component object EXEC @res = sp_OACreate 'VBScript.RegExp', @obj OUT; -- Apply/set the pattern to the RegEx object EXEC @res = sp_OASetProperty @obj, 'Pattern', @pattern; -- Set any other settings/properties here EXEC @res = sp_OASetProperty @obj, 'IgnoreCase', 1; -- Call the method 'Test' to find a match EXEC @res = sp_OAMethod @obj, 'Test', @match OUT, @matchstring; -- Don't forget to clean-up EXEC @res = sp_OADestroy @obj; 

如果SQL Server blocked access to procedure 'sys.sp_OACreate'...错误,请使用sp_reconfigure启用Ole Automation Procedures 。 (是的,不幸的是,这是一个服务器级别的变化!)

有关Test方法的更多信息,请点击这里

快乐的编码

如果您使用的是SQL Server 2016或更高版本,则可以将sp_execute_external_script与R一起使用。它具有正则expression式search的function,例如grepgrepl

这是一个电子邮件地址的例子。 我将通过SQL Server数据库引擎查询一些“人员”,将这些人员的数据传递给R,让R决定哪些人有无效的电子邮件地址,并让R将这些人员的子集传递给SQL Server。 “人员”来自[WideWorldImporters]示例数据库中的[Application].[People]表。 它们作为名为InputDataSet的数据InputDataSet传递给R引擎。 R使用带有“not”操作符(感叹号!)的grepl函数来查找哪些人的电子邮件地址与RegExstringsearch模式不匹配。

 EXEC sp_execute_external_script @language = N'R', @script = N' RegexWithR <- InputDataSet; OutputDataSet <- RegexWithR[!grepl("([_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[az]{2,4}))", RegexWithR$EmailAddress), ];', @input_data_1 = N'SELECT PersonID, FullName, EmailAddress FROM Application.People' WITH RESULT SETS (([PersonID] INT, [FullName] NVARCHAR(50), [EmailAddress] NVARCHAR(256))) 

请注意,相应的function必须安装在SQL Server主机上。 对于SQL Server 2016,它被称为“SQL Server R服务”。 对于SQL Server 2017,它被重命名为“SQL Server Machine Learning Services”。

最后的想法微软的SQL(T-SQL)实现没有本地支持RegEx。 对于OP来说,这个build议的解决scheme可能比使用CLR存储过程更不理想。 但它确实提供了一种解决问题的方法。