如何删除以某些词开头的句子?

时间:2021-10-12 12:46:35

Using PL/SQL, how can I remove sentences, except the first occurrence of a sentence that begin with the words This product? Assume every sentence ends with a period (.) and that each sentence begins with an uppercase letter; for example:

使用PL/SQL,我如何删除句子,除了第一个以这个产品中的单词开头的句子?假设每个句子以句点(.)结尾,每个句子以大写字母开头;例如:

 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.';

My attempts:

我的尝试:

 v_desc := regexp_replace(v_desc,'This product*{2,}','') ;
 v_desc := regexp_replace(v_desc,'This product*{2,}*','') ;

Desired results

想要的结果

v_desc := 'This product is used. Product may make you wealthy.';

1 个解决方案

#1


3  

Try this:

试试这个:

 v_desc := regexp_replace(v_desc,'[^^](This product).*[.]', '') ;

Here's the break down:

这里的分解:

[^^] = Not the beginning of the line
(This product).*[.] = Searching for all instances of "This product" followed by a "."

This will end up searching for all Instances of "This product ..." that is not at the beginning of the line.

这将结束搜索“这个产品……”的所有实例,该实例不在行首。

*Edit after comment

*编辑评论后

Then what you want is a positive look behind (?<=). It should look like this:

那么你想要的是一个积极的态度(?<=)。它应该是这样的:

 v_desc := regexp_replace(v_desc,'(?<=(This product).*)(This product).*[.]', '') ;

Breakdown:

分解:

(?<=(This product).*) = Positive lookbehind.  
(This product).*[.] = What is being searched for

It will only trigger the next value as true if the expression "This product ..." is before it. This will prevent the first occurrence of "This product" from showing up in the values to be replaced.

它只会触发下一个值,如果表达式“这个产品…”在它之前。这将防止“该产品”首次出现在要替换的值中。

I would like to point out, that the lookaheads and lookbehinds are not supported on all implantation of Regex. I don't know the specifics of PL/SQL, so I cannot guarantee this will work. It does work when I test on Regex Hero.

我想指出的是,lookahead和lookbehind并不支持Regex的所有植入。我不知道PL/SQL的具体细节,所以我不能保证它能工作。当我测试Regex Hero时,它确实有效。

#1


3  

Try this:

试试这个:

 v_desc := regexp_replace(v_desc,'[^^](This product).*[.]', '') ;

Here's the break down:

这里的分解:

[^^] = Not the beginning of the line
(This product).*[.] = Searching for all instances of "This product" followed by a "."

This will end up searching for all Instances of "This product ..." that is not at the beginning of the line.

这将结束搜索“这个产品……”的所有实例,该实例不在行首。

*Edit after comment

*编辑评论后

Then what you want is a positive look behind (?<=). It should look like this:

那么你想要的是一个积极的态度(?<=)。它应该是这样的:

 v_desc := regexp_replace(v_desc,'(?<=(This product).*)(This product).*[.]', '') ;

Breakdown:

分解:

(?<=(This product).*) = Positive lookbehind.  
(This product).*[.] = What is being searched for

It will only trigger the next value as true if the expression "This product ..." is before it. This will prevent the first occurrence of "This product" from showing up in the values to be replaced.

它只会触发下一个值,如果表达式“这个产品…”在它之前。这将防止“该产品”首次出现在要替换的值中。

I would like to point out, that the lookaheads and lookbehinds are not supported on all implantation of Regex. I don't know the specifics of PL/SQL, so I cannot guarantee this will work. It does work when I test on Regex Hero.

我想指出的是,lookahead和lookbehind并不支持Regex的所有植入。我不知道PL/SQL的具体细节,所以我不能保证它能工作。当我测试Regex Hero时,它确实有效。