Search code examples
sqloracle-databaseregexp-replace

replace anything between a start string and end string (including special char) using REGEXP_REPLACE


update customer set cust_info= REGEXP_REPLACE(cust_info,'<start><cust_name><start><cust_name><this field has long string><end>','') where user_id=123;

ORA-12733: regular expression too long

so i tried with REGEXP_REPLACE(cust_info,'<start>[A-Z0-9_](*.?)<end>',''); but didn't worked.

I would like to replace anything between <start> string and <end> string to blank. (i.e remove anything between<start> and <end>).

PS:- cust_info column contain long string with html tags.


Solution

  • Your regex seems not to be okay try expression <start>(.)*?<end>

    WITH da AS ( 
    SELECT '<start><cust_name><start><cust_name><this field has long string><end>' AS cust_info FROM dual UNION ALL
    SELECT 'name_test' AS cust_info FROM dual
    ) SELECT REGEXP_REPLACE(cust_info,'<start>(.)*?<end>','') FROM da;
    

    Try

    UPDATE
        customer
    SET
        cust_info = REGEXP_REPLACE(cust_info, '<start>(.)*?<end>', '')
    WHERE
        user_id = 123;
    

    Explanation:-

    <start> //Matches literal <start>
        (.) //Matches any character except linebreaks
         *  //Matches 0 or more of the preceding token of (.)
         ?  //Makes the preceding quantifier lazy, causing it to match as few characters as possible
    <end>   //Matches literal <end> 
    

    In case your string has line breaks use the match_parameter to put it into concideration

    REGEXP_REPLACE ( cust_info, '<start>(.*?)*?<end>' , '' , 1 , 1 , 'n'   ) 
    

    Based on:

    REGEXP_REPLACE ( source_string, search_pattern
                     [, replacement_string
                        [, star_position
                           [, nth_occurrence
                              [, match_parameter ]
                           ]
                        ]
                     ]
                   )
    

    Therefore:

    UPDATE
        customer
    SET
        cust_info = REGEXP_REPLACE ( ID_DESC, '<start>(.*?)*?<end>' , '' , 1 , 1 , 'n'   )
    WHERE
        user_id = 123;