Oracle - 使用正则表达式从字符串中删除尾随字符

时间:2014-07-23 11:17:39

标签: regex oracle

我想使用Oracle正则表达式从字符串中删除尾随字符。

我的数据如下:

TDC Supervisor #
TDC Supervisor ##
TDC Supervisor ###
Store Manager - 1
Store Manager - 2
Store Manager - 3
Stockroom Clerk Flexi 100 Hrs
Sales Consultant Flexi 120 Hrs
Service Desk Consultant (5 Day)
Secretary

我想摆脱尾随的#- 1,而不是120 Hrs5 Day。有没有办法使用Oracle正则表达式执行此操作。不确定我是应该使用REGEXP_SUBSTR还是REGEXP_REPLACE

所以基本上我想做一个独特的回报:

TDC Supervisor
Store Manager
Stockroom Clerk Flexi 100 Hrs
Sales Consultant Flexi 120 Hrs
Service Desk Consultant (5 Day)
Secretary

2 个答案:

答案 0 :(得分:2)

最后的哈希值已移除#+$(哈希#,一个或多个+,字符串结尾$

这些号码会被- *\d+$删除,-表示减号,*表示零个或多个空格,\d+表示一个或多个数字。

您可以将它们与parens结合使用:(#+$)|(- *\d+$)

使用*(或\s*删除零个或多个空格以捕获其他空格,如标签等),因此最终的正则表达式看起来像\s*(#+$)|(-\s*\d+$)

CREATE TABLE mytable (mycolumn VARCHAR2(50));

INSERT INTO mytable VALUES ('TDC Supervisor #');
INSERT INTO mytable VALUES ('TDC Supervisor ##');
INSERT INTO mytable VALUES ('TDC Supervisor ###');
INSERT INTO mytable VALUES ('Store Manager - 1');
INSERT INTO mytable VALUES ('Store Manager - 2');
INSERT INTO mytable VALUES ('Store Manager - 3');
INSERT INTO mytable VALUES ('Stockroom Clerk Flexi 100 Hrs');
INSERT INTO mytable VALUES ('Sales Consultant Flexi 120 Hrs');
INSERT INTO mytable VALUES ('Service Desk Consultant (5 Day)');
INSERT INTO mytable VALUES ('Secretary');

select distinct regexp_replace(mycolumn,'\s*(#+$)|(-\s*\d+$)') 
  from mytable;

Service Desk Consultant (5 Day)
Store Manager
TDC Supervisor 
Secretary
Sales Consultant Flexi 120 Hrs
Stockroom Clerk Flexi 100 Hrs

答案 1 :(得分:1)

regexp_replace(string, '\s*(-\s*\d+|#+)$')
fiddle