用PLSQL中的REGEXP_REPLACE替换所有实例

时间:2015-04-07 00:00:26

标签: regex plsql

我一直在尝试使用PLSQL中的REGEXP_REPLACE替换给定字符串中关键字 POSTPONE 周围的所有文本。

如果有两个 POSTPONE 注释彼此相邻,则仅替换frist实例。

以下是我一直在努力的例子

CROWHURA_POSTPONE_26/03/2015_Postpone_1 30/03/2015 00:00:00~CAMERONR_POSTPONE_30/03/2015_Postpone_1 04/04/2015 00:00:00~CAMERONR_NOTES_30/03/2015_waiting on sr results~CHINGD_POSTPONE_04/04/2015_Postpone_1 07/04/2015 00:00:00

我只想要与__NOTE_

相关的文字
#CAMERONR_NOTES_30/03/2015_waiting on sr results# 

我一直在使用的REGEXP是:

REGEXP_REPLACE(q.comments,'(~|^).{0,10}_POSTPONE_.{1,42}(~|$)','# ')

非常感谢任何帮助。

编辑: 进一步的例子,替换需要工作: 对于其中的每一个,我想要任何和所有_note_相关的文本

VANHOOYJ_NOTES_27/03/2015_Site switching out ~VANHOOYJ_POSTPONE_27/03/2015_Postpone_1 03/04/2015 00:00:00
FABISHD_POSTPONE_31/03/2015_Postpone_1 06/04/2015 00:00:00
FABISHD_NOTES_31/03/2015_GE enquiry~FABISHD_POSTPONE_31/03/2015_Postpone_1 06/04/2015 00:00:00
CAMERONR_NOTES_31/03/2015_booked sr ~CAMERONR_POSTPONE_31/03/2015_Postpone_1 05/04/2015 00:00:00
FABISHD_POSTPONE_01/04/2015_Postpone_1 06/04/2015 00:00:00
CHINGD_POSTPONE_01/04/2015_Postpone_1 03/04/2015 00:00:00~CHINGD_POSTPONE_04/04/2015_Postpone_1 07/04/2015 00:00:00
FABISHD_NOTES_31/03/2015_GE enquiry~FABISHD_POSTPONE_31/03/2015_Postpone_1 06/04/2015 00:00:00
CHINGD_NOTES_01/04/2015_waiting on switching~CHINGD_POSTPONE_01/04/2015_Postpone_1 06/04/2015 00:00:00
FABISHD_NOTES_19/03/2015_Needs to be completed by switching~FABISHD_POSTPONE_19/03/2015_Postpone_1 26/03/2015 00:00:00~FABISHD_NOTES_26/03/2015_Switching~FABISHD_POSTPONE_26/03/2015_Postpone_1 30/03/2015 00:00:00~CHINGD_POSTPONE_30/03/2015_Postpone_1 06/04/2015 00:00:00
CHINGD_NOTES_27/03/2015_GEN2360163 to confirm read~CHINGD_POSTPONE_27/03/2015_Postpone_1 01/04/2015 00:00:00~CHINGD_POSTPONE_29/03/2015_Postpone_1 02/04/2015 00:00:00~CHINGD_POSTPONE_04/04/2015_Postpone_1 07/04/2015 00:00:00
CHINGD_POSTPONE_29/03/2015_Postpone_1 02/04/2015 00:00:00~CHINGD_POSTPONE_04/04/2015_Postpone_1 07/04/2015 00:00:00
CHINGD_NOTES_01/04/2015_waiting on switching~CHINGD_POSTPONE_01/04/2015_Postpone_1 06/04/2015 00:00:00
FABISHD_NOTES_30/03/2015_Unsure where to credit units to.~FABISHD_POSTPONE_30/03/2015_Postpone_1 31/03/2015 00:00:00~CAMERONR_POSTPONE_31/03/2015_Postpone_1 05/04/2015 00:00:00

1 个答案:

答案 0 :(得分:1)

SQL Fiddle

**查询**:

select val,
regexp_replace(val, '(~?([^~]+_NOTES_[^~]+)~?)|(.)','#\2#') x,  --keeps the notes part and replaces everything esle with #
regexp_replace(regexp_replace(val, '(~?([^~]+_NOTES_[^~]+)~?)|(.)','#\2#'),'#{2,}','#') y  --removes consecutive #s
from
test t

<强> Results

|                                                                                                                                                                                                                                     VAL |                                                                                                                                                                                                                                                                                                                                                                                                                      X |                                                                                                Y |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
|                                                                                                                                                                      VANHOOYJ_NOTES_27/03/2015_Site switching out ~VANHOOYJ_POSTPONE_27 |                                                                                                                                                                                                                                                                                         #VANHOOYJ_NOTES_27/03/2015_Site switching out ################################################################################ |                                                  #VANHOOYJ_NOTES_27/03/2015_Site switching out # |
|                                                                                                                                                                                                                     FABISHD_POSTPONE_31 |                                                                                                                                                                                                                                                                                                                                          ############################################################################# |                                                                                                # |
|                                                                                                                                                                                 FABISHD_NOTES_31/03/2015_GE enquiry~FABISHD_POSTPONE_31 |                                                                                                                                                                                                                                                                                                     #FABISHD_NOTES_31/03/2015_GE enquiry############################################################################## |                                                            #FABISHD_NOTES_31/03/2015_GE enquiry# |
|                                                                                                                                                                               CAMERONR_NOTES_31/03/2015_booked sr ~CAMERONR_POSTPONE_31 |                                                                                                                                                                                                                                                                                                  #CAMERONR_NOTES_31/03/2015_booked sr ################################################################################ |                                                           #CAMERONR_NOTES_31/03/2015_booked sr # |
|                                                                                                                                                                                                                     FABISHD_POSTPONE_01 |                                                                                                                                                                                                                                                                                                                                          ############################################################################# |                                                                                                # |
|                                                                                                                                                            CHINGD_POSTPONE_01/04/2015_Postpone_1 03/04/2015 00:00:00~CHINGD_POSTPONE_04 |                                                                                                                                                                                                                        ############################################################################################################################################################################################### |                                                                                                # |
|                                                                                                                                                                                 FABISHD_NOTES_31/03/2015_GE enquiry~FABISHD_POSTPONE_31 |                                                                                                                                                                                                                                                                                                     #FABISHD_NOTES_31/03/2015_GE enquiry############################################################################## |                                                            #FABISHD_NOTES_31/03/2015_GE enquiry# |
|                                                                                                                                                                         CHINGD_NOTES_01/04/2015_waiting on switching~CHINGD_POSTPONE_01 |                                                                                                                                                                                                                                                                                              #CHINGD_NOTES_01/04/2015_waiting on switching############################################################################ |                                                   #CHINGD_NOTES_01/04/2015_waiting on switching# |
| FABISHD_NOTES_19/03/2015_Needs to be completed by switching~FABISHD_POSTPONE_19/03/2015_Postpone_1 26/03/2015 00:00:00~FABISHD_NOTES_26/03/2015_Switching~FABISHD_POSTPONE_26/03/2015_Postpone_1 30/03/2015 00:00:00~CHINGD_POSTPONE_30 | #FABISHD_NOTES_19/03/2015_Needs to be completed by switching######################################################################################################################FABISHD_NOTES_26/03/2015_Switching################################################################################################################################################################################################## | #FABISHD_NOTES_19/03/2015_Needs to be completed by switching#FABISHD_NOTES_26/03/2015_Switching# |
|                                               CHINGD_NOTES_27/03/2015_GEN2360163 to confirm read~CHINGD_POSTPONE_27/03/2015_Postpone_1 01/04/2015 00:00:00~CHINGD_POSTPONE_29/03/2015_Postpone_1 02/04/2015 00:00:00~CHINGD_POSTPONE_04 |                                                #CHINGD_NOTES_27/03/2015_GEN2360163 to confirm read#################################################################################################################################################################################################################################################################################################################### |                                             #CHINGD_NOTES_27/03/2015_GEN2360163 to confirm read# |
|                                                                                                                                                            CHINGD_POSTPONE_29/03/2015_Postpone_1 02/04/2015 00:00:00~CHINGD_POSTPONE_04 |                                                                                                                                                                                                                        ############################################################################################################################################################################################### |                                                                                                # |
|                                                                                                                                                                         CHINGD_NOTES_01/04/2015_waiting on switching~CHINGD_POSTPONE_01 |                                                                                                                                                                                                                                                                                              #CHINGD_NOTES_01/04/2015_waiting on switching############################################################################ |                                                   #CHINGD_NOTES_01/04/2015_waiting on switching# |
|                                                                                               FABISHD_NOTES_30/03/2015_Unsure where to credit units to.~FABISHD_POSTPONE_30/03/2015_Postpone_1 31/03/2015 00:00:00~CAMERONR_POSTPONE_31 |                                                                                                                                                       #FABISHD_NOTES_30/03/2015_Unsure where to credit units to.###################################################################################################################################################################################################### |                                      #FABISHD_NOTES_30/03/2015_Unsure where to credit units to.# |

感谢@Jorge Campos为sqlfiddle。