SQL Server通过外部程序集注册正则表达式函数(CLR函数) [转]

转自:http://blog.csdn.net/binguo168/article/details/76598581

1.下载dll程序集(通过C#编写的支持正则的方法),百度网盘下载:

1.1如果只想用,可以直接下载MSSQLRegexExtend.dll
https://pan.baidu.com/s/1qX8eHa8
1.2正则程序集对应的解决方案MSSQLRegexExtend.sln,已打包
https://pan.baidu.com/s/1qXZja9m

2.SQL Server数据库注册程序集

[sql] view plain copy
 
  1. CREATE ASSEMBLY AssemblyRegex from 'D:MSSQLRegexExtendMSSQLRegexExtend.dll'   
  2. WITH PERMISSION_SET = SAFE   
3.设置开启支持CLR  
[sql] view plain copy
 
  1. EXEC SP_CONFIGURE 'clr enabled', 1   
  2. RECONFIGURE    <strong style="color: rgb(255, 0, 0);"> </strong>  
4.创建支持正则匹配的标量函数 
[sql] view plain copy
 
  1. --DROP FUNCTION [dbo].[RegexMatch]    
  2. CREATE FUNCTION [dbo].[RegexMatch](@Regex [nvarchar](max),@Input [nvarchar](max))    
  3. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER    
  4. AS     
  5. EXTERNAL NAME [AssemblyRegex].[MSSQLRegexExtend.RegexExtend].[Match]    
5.创建支持正则替换的标量函数 
[sql] view plain copy
 
  1. --DROP FUNCTION [dbo].[RegexReplace]    
  2. CREATE FUNCTION [dbo].[RegexReplace](@Regex [nvarchar](max),@Input [nvarchar](max),@Replace [nvarchar](max))    
  3. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER    
  4. AS     
  5. EXTERNAL NAME [AssemblyRegex].[MSSQLRegexExtend.RegexExtend].[Replace]    
6.创建支持正则校验的标量函数
[sql] view plain copy
 
  1. --DROP FUNCTION [dbo].[RegexIsMatch]    
  2. CREATE FUNCTION [dbo].[RegexIsMatch](@Regex [nvarchar](max),@Input [nvarchar](max))    
  3. RETURNS [bit] WITH EXECUTE AS CALLER    
  4. AS     
  5. EXTERNAL NAME [AssemblyRegex].[MSSQLRegexExtend.RegexExtend].[IsMatch]    

附部分简单正则:
[sql] view plain copy
 
  1. /*  
  2.   
  3. .  匹配除换行符以外的任意字符   
  4. w 匹配字母或数字或下划线或汉字   
  5. s 匹配任意的空白符   
  6. d 匹配数字   
  7.  匹配单词的开始或结束   
  8. ^  匹配字符串的开始   
  9. $  匹配字符串的结束   
  10.   
  11. */  
  12.   
  13. /*  
  14.   
  15. *     重复零次或更多次   
  16. +     重复一次或更多次   
  17. ?     重复零次或一次   
  18. {n}   重复n次   
  19. {n,}  重复n次或更多次   
  20. {n,m} 重复n到m次   
  21.   
  22. W       匹配任意不是字母,数字,下划线,汉字的字符   
  23. S       匹配任意不是空白符的字符   
  24. D       匹配任意非数字的字符   
  25. B       匹配不是单词开头或结束的位置   
  26. [^x]     匹配除了x以外的任意字符   
  27. [^aeiou] 匹配除了aeiou这几个字母以外的任意字符   
  28.   
  29.   
  30.   
  31. IP地址匹配: ((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)  
  32.   
  33. (d{1,3}.){3}d{1,3}是一个简单的IP地址匹配表达式。表达式顺序分析:   
  34.    
  35. d{1,3}匹配1到3位的数字,  
  36.   
  37. (d{1,3}.){3}匹配三位数字加上一个英文句号(这个整体也就是这个分组)重复3次,  
  38.   
  39. 最后再加上一个一到三位的数字(d{1,3})。  
  40.   
  41. */  



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