使用 PL/SQL,我如何删除以单词 This product
开头的句子以外的句子?假设每个句子都以句点 (.
) 结尾,并且每个句子都以大写字母开头;例如:
v_desc varchar2(500);
v_desc := 'This product is used. Product may make you wealthy. This product may appear red. This product is not new.';
我的尝试:
v_desc := regexp_replace(v_desc,'This product*{2,}','') ;
v_desc := regexp_replace(v_desc,'This product*{2,}*','') ;
期望的结果
v_desc := 'This product is used. Product may make you wealthy.';
最佳答案
试试这个:
v_desc := regexp_replace(v_desc,'[^^](This product).*[.]', '') ;
分解如下:
[^^] = Not the beginning of the line
(This product).*[.] = Searching for all instances of "This product" followed by a "."
这将最终搜索所有不在行首的“This product ...”实例。
*评论后编辑
那你要的是背后的正面看(?<=)
.它应该看起来像这样:
v_desc := regexp_replace(v_desc,'(?<=(This product).*)(This product).*[.]', '') ;
分割:
(?<=(This product).*) = Positive lookbehind.
(This product).*[.] = What is being searched for
如果表达式“This product ...”在它之前,它只会将下一个值触发为 true。这将防止第一次出现的“This product”出现在要替换的值中。
我想指出,并非所有 Regex 植入都支持先行和后行。我不知道 PL/SQL 的细节,所以我不能保证这会起作用。当我在 Regex Hero 上测试时它确实有效.
关于regex - 如何删除以特定单词开头的句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806316/