Sample code for 30+ languages & platforms
SQL Server

Regular Expression Replace Full Matches

See more Regular Expressions Examples

Demonstrates replacing the full matches of a regular expression.

Note: Chilkat uses PCRE2. See PCRE2 Regular Expressions
Also see: PCRE2 Performance

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    SELECT @success = 0

    DECLARE @subject nvarchar(4000)
    SELECT @subject = 'John Anders, +_+_+ Mary Robins $$$$'
    DECLARE @pattern nvarchar(4000)
    SELECT @pattern = '\w+\s+\w+'

    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @sb, 'Append', @success OUT, @subject

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OASetProperty @json, 'EmitCompact', 0

    DECLARE @timeoutMs int
    SELECT @timeoutMs = 2000
    DECLARE @numMatches int
    EXEC sp_OAMethod @sb, 'RegexMatch', @numMatches OUT, @pattern, @json, @timeoutMs
    IF @numMatches < 0
      BEGIN
        -- Probably an error in the regular expression.
        -- Suggestion: Use AI to help create and/or diagnose regular expressions.
        EXEC sp_OAGetProperty @sb, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    -- Examine the matches:
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- {
    --   "match": [
    --     {
    --       "group": [
    --         {
    --           "cap": "John Anders",
    --           "idx": 0,
    --           "len": 11
    --         }
    --       ]
    --     },
    --     {
    --       "group": [
    --         {
    --           "cap": "Mary Robins",
    --           "idx": 19,
    --           "len": 11
    --         }
    --       ]
    --     }
    --   ]
    -- }

    DECLARE @sbTemp int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbTemp OUT

    DECLARE @i int
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @numMatches OUT, 'match'
    WHILE @i < @numMatches
      BEGIN

        EXEC sp_OASetProperty @json, 'I', @i

        -- The full match is always in group 0.
        EXEC sp_OAMethod @sbTemp, 'Clear', NULL
        EXEC sp_OAMethod @json, 'StringOfSb', @success OUT, 'match[i].group[0].cap', @sbTemp

        -- Indicate that we wish to replace the full match with it's value converted to uppercase.
        EXEC sp_OAMethod @sbTemp, 'ToUppercase', @success OUT
        EXEC sp_OAMethod @json, 'UpdateSb', @success OUT, 'match[i].group[0].rep', @sbTemp

        SELECT @i = @i + 1
      END

    -- The JSON now has replacement strings:
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- {
    --   "match": [
    --     {
    --       "group": [
    --         {
    --           "cap": "John Anders",
    --           "idx": 0,
    --           "len": 11,
    --           "rep": "JOHN ANDERS"
    --         }
    --       ]
    --     },
    --     {
    --       "group": [
    --         {
    --           "cap": "Mary Robins",
    --           "idx": 19,
    --           "len": 11,
    --           "rep": "MARY ROBINS"
    --         }
    --       ]
    --     }
    --   ]
    -- }

    -- Call RegexReplace to update the StringBuilder with the replacements.
    EXEC sp_OAMethod @sb, 'RegexReplace', @success OUT, @json
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sb, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sb
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbTemp
        RETURN
      END


    PRINT 'Result after doing replacements:'
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Result after doing replacements:
    -- JOHN ANDERS, +_+_+ MARY ROBINS $$$$

    EXEC @hr = sp_OADestroy @sb
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbTemp


END
GO