Using regular expression functions in T-SQL _mssql

Source: Internet
Author: User
First, we create a database Project in VSTS, add a class, and implement one of the following methods:
Copy Code code as follows:

<summary>
Regs the ex match.
</summary>
<param name= "inputvalue" >the input value.</param>
<param name= "Regexpattern" >the regex pattern.</param>
<remarks>author:petter Liu http://wintersun.cnblogs.com </remarks>
<returns>1 match,0 not match</returns>
[SqlFunction]
public static bool Regexmatch (string inputvalue, String regexpattern)
{
Any nulls-we can ' t match, return false
if (string. IsNullOrEmpty (inputvalue) | | String. IsNullOrEmpty (Regexpattern))
return false;

Regex r1 = new Regex (Regexpattern.trimend (null));
return R1. Match (Inputvalue.trimend (null)). Success;
}

OK, after build deploy to your target database OK, Visual Studio will automatically register the assembly. If you want to register the assembly manually, you can perform the following T-sql:
Copy Code code as follows:

CREATE ASSEMBLY [regexclr] from ' RegExCLR.dll ';

--Add the REGEX function. We want a friendly name
--Regexmatch rather than the full namespace name.
--the way we have to specify the Assembly.Namespace.Class.Function
--Note the REGEXCLR.REGEXCLR
--(one is the assembly the "is the" namespace)
CREATE FUNCTION Regexmatch (@inputCalue NVARCHAR (4000),
@regexPattern NVARCHAR (4000)) RETURNS BIT
As EXTERNAL NAME RegExCLR.RegExCLR.ClrClass.RegExMatch;

OK, all OK, after we test it:

Select COUNT (1) from Threads where dbo. Regexmatch (ThreadId, ' ^[{|\ (]?[ 0-9a-fa-f]{8}[-]? ([0-9a-fa-f]{4}[-]?) {3} [0-9a-fa-f] {12} [\)|}]? $ ') =1
The T-SQL above is the number of records that find the Threads table ThreadID is a GUID. equals 1 is a match, ^[{|\ (]?[ 0-9a-fa-f]{8}[-]? ([0-9a-fa-f]{4}[-]?) {3} [0-9a-fa-f] {12} [\)|}]? $ regular expression that matches the GUID.

I hope this post will be helpful to you.

You may be interested in the following post:

The performance comparison between the SQLSERVER2008 of CTE and the CLR

SQL Server uses CLR Stored procedure to export data to Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.