【问题标题】:replace anything between a start string and end string (including special char) using REGEXP_REPLACE使用 REGEXP_REPLACE 替换开始字符串和结束字符串(包括特殊字符)之间的任何内容
【发布时间】:2020-06-14 18:03:18
【问题描述】:

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 标签的长字符串。

【问题讨论】:

  • 如果您编辑原始帖子以显示您正在使用的实际数据以及您期望的结果,这将有所帮助。

标签: sql oracle regexp-replace


【解决方案1】:

您的正则表达式似乎不太好尝试表达式<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;

【讨论】:

  • 请解释一下 (.)*?也是。
  • 我认为 1 , 1 , 'n' ) 在这里没有必要,因为 [, star_position [, nth_occurrence [, match_parameter ] 默认为 1。
  • @Roger 是的,是的
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多