从plsql中的字符串中删除前导和尾随单词

时间:2016-07-08 11:05:52

标签: plsql

以下是字符串,我想从字符串中删除前导和尾随'和'字。

String:和121630是ABC 9.0.2.0的一部分,121630包含在DEF和

预期产出:121630是ABC 9.0.2.0的一部分,121630包含在DEF中

任何人都可以告诉我该怎么做?

3 个答案:

答案 0 :(得分:1)

SQL> with tbl(str) as (
      select 'and 121630 is part of ABC 9.0.2.0 and 121630 is included in DEF and' 
      from dual
    )
    select regexp_replace(str, '^and (.*) and$', '\1') fixed_str
    from tbl;

FIXED_STR
-----------------------------------------------------------
121630 is part of ABC 9.0.2.0 and 121630 is included in DEF

SQL>

答案 1 :(得分:0)

您可以使用LTRIM和RTRIM功能:

declare 
  lString VARCHAR2(1000);
begin
  lString := 'and 121630 is part of ABC 9.0.2.0 and 121630 is included in DEF and';

  lString := ltrim(lString,'and ');

  lString := rtrim(lString,' and');

  dbms_output.put_line(lString);
end;

答案 2 :(得分:0)

我不是regular expression的专家,但这可以工作:):

SELECT REGEXP_REPLACE (' and 121630 is part of ABC 9.0.2.0 and 121630 is included in DEF and', 
                       '(^\s*and\s*(\s|$))|((^|\s)\s*and\s*$)')  
  FROM DUAL

编辑:  对于多个'和'组成一个小组:

SELECT REGEXP_REPLACE (' and  and   and 121630 is part of ABC 9.0.2.0 and 121630 is included in DEF', 
                       '(^(\s*and\s*)+(\s|$))|((^|\s)(\s*and\s*)+$)')  
  FROM DUAL