请求正则表达式从Oracle中的字符串中删除前缀或后缀

时间:2014-03-27 07:50:03

标签: regex plsql oracle11g

我想从字符串中删除前缀或后缀,字符串如下所示:
L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z|
|L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z|||
我想在字符串的结尾或开头删除分隔符|,在最后,字符串应该如下所示:
L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z

那么,如何在PLSQL中编写正则表达式呢?

2 个答案:

答案 0 :(得分:1)

这个正则表达式完成了这项工作:

select regexp_replace('|L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z|||','(^[\|]*)|[\|]*$') from dual

答案 1 :(得分:1)

正如EatÅPeach建议的那样,你不需要使用正则表达式,内置的TRIM function会这样做:

select trim('|' from '|L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z|||')
from dual;

TRIM('|'FROM'|L-CHGSUP-CDO-BR-NET|MSCOORDINATORX|MSCOORDINATORZ|||')
--------------------------------------------------------------------
L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z   

或者如果您已经拥有该值并希望在PL / SQL中修剪它(作为一个人为的例子):

set serveroutput on
declare
  old_value varchar2(80);
  new_value varchar2(80);
begin
  old_value := '|L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z|||';
  new_value := trim('|' from old_value);
  dbms_output.put_line(new_value);
end;
/

anonymous block completed
L-CHGSUP-CDO-BR-NET|MS COORDINATOR X|MS COORDINATOR Z