Using regular expressions in SQL Server

Source: Internet
Author: User
Tags ole

CREATE FUNCTIONDbo.find_regular_expression (@source varchar( the),--a matching source string is required  @regexp varchar( +),--Regular Expressions  @ignorecase bit = 0  --is case-sensitive, false by default)RETURNS bit  --return result 0-false,1-true asBEGIN--0 (Success) or not 0 digits (failed) is an integer value of the HRESULT returned by the OLE Automation object.   DECLARE @hr integer --used to save the returned object token for subsequent manipulation of the object  DECLARE @objRegExp integer   DECLARE @objMatches integer--Save Results  DECLARE @results bit  /*Create an OLE object instance, only members of the sysadmin fixed server role can perform sp_oacreate and determine if there is a VBScript.RegExp class library in the machine*/  EXEC @hr =sp_OACreate'VBScript.RegExp',@objRegExpOUTPUTIF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  END/*The following three are the three properties for setting up a new object.     The following are examples of properties commonly used in ' vbscript.regexp ': Dim regex,match,matches ' build variables.     Set regEx = New RegExp ' establishes a general expression.     regex.pattern= patrn ' setup mode.     Regex.ignorecase = True ' setting is case-sensitive.     Regex.global=true ' Set global availability.    Set Matches=regex.execute (String) ' duplicate match set regexptest = Regex.execute (strng) ' performs the search. For each match in matches ' duplicate match collection Retstr=retstr & "match found at position" retstr=retstr&am P match.firstindex& ". Match Value is ' "retstr=retstr&match.value&" '. " &vbcrlf Next regexptest=retstr*/  EXEC @hr =sp_OASetProperty@objRegExp,'Pattern',@regexp  IF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  END  EXEC @hr =sp_OASetProperty@objRegExp,'Global', FalseIF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  END  EXEC @hr =sp_OASetProperty@objRegExp,'IgnoreCase',@ignorecase  IF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  END --Calling Object Methods  EXEC @hr =sp_OAMethod@objRegExp,'Test',@resultsOUTPUT,@source  IF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  END--to release an OLE object that has been created  EXEC @hr =sp_OADestroy@objRegExp  IF @hr <> 0 BEGIN   SET @results = 0   RETURN @results  ENDRETURN @resultsEND

Using regular expressions in SQL Server

Related Article

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.