I. New. NET Class Library project
- Create a class library project named Mssqlregexextend
- Create a class named Regexextend
- Copy the following code into the class[CSharp]View PlainCopy
- Using System.Text.RegularExpressions;
- Namespace Mssqlregexextend
- {
- public class Regexextend
- {
- // <summary>
- /// regular match
- // </summary>
- /// <param name= "regex" > Regular Expressions </param>
- /// <param name= "input" > Text </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>
- /// regular replacement
- // </summary>
- /// <param name= "regex" > Regular Expressions </param>
- /// <param name= "input" > Text </param>
- /// <param name= "Replace" > Target to 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>
- /// regular check
- // </summary>
- /// <param name= "regex" > Regular Expressions </param>
- /// <param name= "input" > Text </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);
- }
- }
- }
- Right-click the project Build
Second, register the class library in MSSQL
Execute the following script in the database (the Class inventory drop address has to be modified correctly).
[SQL]View PlainCopy
- --drop ASSEMBLY Regex
- CREATE ASSEMBLY Regex from ' E:\CSharp\MSSQLRegexExtend\MSSQLRegexExtend\bin\Release\MSSQLRegexExtend.dll ' with Permission_set = SAFE --Register the. NET class Library
- sp_configure ' clr enabled ', 1 --Set the database to use CLR components
- RECONFIGURE --Set the available CLR components. Don't forget to run this line to apply
- /**** The following code registers a static method in the class library as a function ****/
- /**** regular Match ****/
- --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]
- /**** Regular Replacement ****/
- --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]
- /**** Regular Check ****/
- --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]
Iii. Invocation of the example
[SQL]View PlainCopy
- SELECT [CustomerID]
- , [CompanyName]
- , [ContactName]
- , [ContactTitle]
- , [City]
- , [Region]
- , [PostalCode]
- , [country]
- , [Phone]
- , [Fax]
- , [Address]
- , [dbo]. [Regex.match] (' (\d) + ', [Address]) as [house number] --Regular match
- , [dbo]. [Regex. Replace] (' \d ', [Address],' * ') as [code the house number] --Regular replacement
- From [Northwind]. [dbo]. [Customers]
- where [dbo]. [Regex.IsMatch] (' \d ', [Address]) =1 --Check the record of the house number
Using regular expressions in SQL Server