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