简体   繁体   中英

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. (ie remove anything between <start> and <end> ).

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM