zoukankan      html  css  js  c++  java
  • SQLserver中使用正则表达式

    一、新建.net类库项目

    1. 创建类库项目,名为MSSQLRegexExtend
    2. 创建一个类,名为RegexExtend
    3. 复制下面代码到类中
      [csharp] view plain copy
       
      1. using System.Text.RegularExpressions;  
      2.   
      3. namespace MSSQLRegexExtend  
      4. {  
      5.   
      6.     public class RegexExtend  
      7.     {  
      8.         /// <summary>  
      9.         /// 正则匹配  
      10.         /// </summary>  
      11.         /// <param name="regex">正则表达式</param>  
      12.         /// <param name="input">文本</param>  
      13.         /// <returns></returns>  
      14.         [Microsoft.SqlServer.Server.SqlFunction]  
      15.         public static string Match(string regex, string input)  
      16.         {  
      17.             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Value;  
      18.         }  
      19.   
      20.         /// <summary>  
      21.         /// 正则替换  
      22.         /// </summary>  
      23.         /// <param name="regex">正则表达式</param>  
      24.         /// <param name="input">文本</param>  
      25.         /// <param name="replace">要替换的目标</param>  
      26.         /// <returns></returns>  
      27.         [Microsoft.SqlServer.Server.SqlFunction]  
      28.         public static string Replace(string regex, string input, string replace)  
      29.         {  
      30.             return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Replace(input, replace);  
      31.         }  
      32.   
      33.         /// <summary>  
      34.         /// 正则校验  
      35.         /// </summary>  
      36.         /// <param name="regex">正则表达式</param>  
      37.         /// <param name="input">文本</param>  
      38.         /// <returns></returns>  
      39.         [Microsoft.SqlServer.Server.SqlFunction]  
      40.         public static bool IsMatch(string regex, string input)  
      41.         {  
      42.             return !string.IsNullOrEmpty(input) && new Regex(regex, RegexOptions.IgnoreCase).IsMatch(input);  
      43.         }  
      44.     }  
      45. }  
    4. 右击项目生成

    二、将类库注册到MSSQL中

    在数据库中执行如下脚本(类库存放地址得自己修改正确)。

    [sql] view plain copy
     
    1. --DROP ASSEMBLY Regex  
    2. CREATE ASSEMBLY Regex from 'E:CSharpMSSQLRegexExtendMSSQLRegexExtendinReleaseMSSQLRegexExtend.dll' WITH PERMISSION_SET = SAFE --注册.net类库  
    3.   
    4. sp_configure 'clr enabled', 1   --将数据库设置为可以使用clr组件  
    5. RECONFIGURE         --设置可用clr组件。别忘记运行这行进行应用  
    6.   
    7. /****以下代码将类库中的静态方法注册为函数****/  
    8.   
    9. /****正则匹配****/  
    10. --DROP FUNCTION [dbo].[Regex.Match]  
    11. CREATE FUNCTION [dbo].[Regex.Match](@Regex [nvarchar](max),@Input [nvarchar](max))  
    12. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER  
    13. AS   
    14. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Match]  
    15.   
    16. /****正则替换****/  
    17. --DROP FUNCTION [dbo].[Regex.Replace]  
    18. CREATE FUNCTION [dbo].[Regex.Replace](@Regex [nvarchar](max),@Input [nvarchar](max),@Replace [nvarchar](max))  
    19. RETURNS [nvarchar](max) WITH EXECUTE AS CALLER  
    20. AS   
    21. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[Replace]  
    22.   
    23. /****正则校验****/  
    24. --DROP FUNCTION [dbo].[Regex.IsMatch]  
    25. CREATE FUNCTION [dbo].[Regex.IsMatch](@Regex [nvarchar](max),@Input [nvarchar](max))  
    26. RETURNS [bit] WITH EXECUTE AS CALLER  
    27. AS   
    28. EXTERNAL NAME [Regex].[MSSQLRegexExtend.RegexExtend].[IsMatch]  

    三、调用示例

    [sql] view plain copy
     
    1. SELECT [CustomerID]  
    2.       ,[CompanyName]  
    3.       ,[ContactName]  
    4.       ,[ContactTitle]  
    5.       ,[City]  
    6.       ,[Region]  
    7.       ,[PostalCode]  
    8.       ,[Country]  
    9.       ,[Phone]  
    10.       ,[Fax]  
    11.       ,[Address]  
    12.       ,[dbo].[Regex.Match]('(d)+',[Address]) as [门牌号码]     --正则匹配  
    13.       ,[dbo].[Regex.Replace]('d',[Address],'*') as [将门牌号码打码]   --正则替换  
    14.   FROM [Northwind].[dbo].[Customers]  
    15.   where [dbo].[Regex.IsMatch]('d',[Address])=1             --正则校验有门牌号码的记录  

     

  • 相关阅读:
    tabhost中setup()和setup(LocalActivityManager activityGroup)
    android自定义TabWidget
    Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
    底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
    TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件
    android的消息处理机制(图+源码分析)——Looper,Handler,Message
    Java高级--Java线程运行栈信息的获取 getStackTrace()
    Java中的守护线程 & 非守护线程(简介)
    Fragment之间的通信
    CString——Left、Right、Find、ReverseFind
  • 原文地址:https://www.cnblogs.com/mingjing/p/7765568.html
Copyright © 2011-2022 走看看