SQL SERVER 正则匹配实例分享--【叶子】

  1. --====================================  
  2. --标题: 应用实例之SQL SERVER 正则匹配  
  3. --作者:maco_wang  
  4. --时间:2012-03-25  
  5. --说明:MS-SQL SERVER 中的正则匹配  
  6. --====================================  
  7.   
  8. /*  
  9. 假设测试数据为:  
  10. col  
  11. ----------  
  12. a b d c e  
  13. a a b c d  
  14. b b c d e  
  15. e u g h w  
  16. o a k d w  
  17.   
  18. 1)得到没有重复字母的行,即想要得到如下结果:  
  19. col  
  20. --------------  
  21. a b c d e  
  22. e u g h w  
  23. o p k n w  
  24.   
  25. 2)得到同时存在a和d,并且a和d之间只有0或是1个字母的,即想要得到的结果:  
  26. col  
  27. ----------  
  28. a b d c e  
  29. o a k d w  
  30.   
  31. */  
  32.   
  33. --测试数据  
  34. if object_id('[tb]'is not null drop table [tb]  
  35. create table [tb] (col varchar(10))  
  36. insert into [tb]  
  37. select 'a b d c e' union all  
  38. select 'a a b c d' union all  
  39. select 'b b c d e' union all  
  40. select 'e u g h w' union all  
  41. select 'o a k d w'   
  42.   
  43. select * from [tb]  
  44.   
  45. --本示例在SQL SERVER 2000版本即可适用。  
  46.   
  47. go  
  48. create function dbo.RegexMatch  
  49. (  
  50.     @pattern varchar(2000),  
  51.     @matchstring varchar(8000)  
  52. )  
  53. returns int  
  54. as   
  55. begin  
  56.     declare @objRegexExp int  
  57.     declare @strErrorMessage varchar(255)  
  58.     declare @hr int,@match bit  
  59.     exec @hr= sp_OACreate 'VBScript.RegExp', @objRegexExp out  
  60.     if @hr = 0   
  61.         exec @hr= sp_OASetProperty @objRegexExp, 'Pattern', @pattern  
  62.     if @hr = 0   
  63.         exec @hr= sp_OASetProperty @objRegexExp, 'IgnoreCase', 1  
  64.     if @hr = 0   
  65.         exec @hr= sp_OAMethod @objRegexExp, 'Test', @match OUT, @matchstring  
  66.     if @hr <>0   
  67.     begin  
  68.         return null  
  69.     end  
  70.     exec sp_OADestroy @objRegexExp  
  71.     return @match  
  72. end  
  73.   
  74. go  
  75. --1)得到没有重复字母的行  
  76. --正常思路,可能是按照空格分割后去重然后合并,最后判断长度(略)  
  77.   
  78. --用正则就很方便了  
  79. select col from [tb] where dbo.RegexMatch('^.*?([a-z])[ ]\1.*?$',col)=0  
  80. /*  
  81. col  
  82. ----------  
  83. a b d c e  
  84. e u g h w  
  85. o a k d w  
  86. */  
  87.   
  88. --2)得到同时存在a和d,并且a和d之间间隔小于等于一个字母的  
  89.   
  90. --正常思路  
  91. select col from [tb]   
  92. where charindex('a',col)>0  
  93. and charindex('d',col)>0  
  94. and abs(charindex('a',col)-charindex('d',col))<5  
  95. /*  
  96. col  
  97. ----------  
  98. a b d c e  
  99. o a k d w  
  100. */  
  101. --正则处理  
  102. select col from [tb] where dbo.RegexMatch('.*a[a-z ]{1,3}d.*',col)=1  
  103. /*  
  104. col  
  105. ----------  
  106. a b d c e  
  107. o a k d w  
  108. */  
  109. --这里的正则写法考虑的就不全,如果d在a前面呢?  
  110. --那么修正一下  
  111. select col from [tb]   
  112. where dbo.RegexMatch('.*d[a-z ]{1,3}a.*|.*a[a-z ]{1,3}d.*',col)=1  
  113. /*  
  114. col  
  115. ----------  
  116. a b d c e  
  117. o a k d w  
  118. */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值