заменить что-либо между начальной строкой и конечной строкой (включая специальный символ), используя REGEXP_REPLACE - PullRequest
2 голосов
/ 02 марта 2020

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: слишком длинное регулярное выражение

, поэтому я попытался с REGEXP_REPLACE(cust_info,'<start>[A-Z0-9_](*.?)<end>','');, но не сработало.

Я хотел бы заменить что-нибудь между <start> строкой и <end> строкой до пустого. (т.е. удалить что-нибудь между <start> и <end>).

PS: - столбец cust_info содержит длинную строку с тегами html.

1 Ответ

2 голосов
/ 02 марта 2020

Ваше регулярное выражение, кажется, не в порядке, попробуйте выражение <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;

Попробуйте

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

Объяснение: -

<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> 

В случае, если ваша строка имеет разрывы строк, используйте параметр match_parameter, чтобы поместить его в заключение

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

На основе:

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

Следовательно:

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