Oracle find to first hyphens after last vertical bar

oracleregexregular expression

oracle find to first hyphens after last vertical bar using REGEXP_REPLACE

SELECT REGEXP_REPLACE ('sadsa|dd|g - g|hh|yyyyy - tttt - ooo', '.*(\|)(?!.*\|).*?\-{1}', '' ) FROM dual;

string input

 'sadsa|dd|g - g|hh|yyyyy - tttt - ooo'

expected output

'sadsa|dd|g - g|hh|yyyyy -'

this regex working on notepad++ but not on oracle

.*(\|)(?!.*\|).*?\-{1}

Best Answer

create table t (
  c varchar2(64)
);

insert into t values ('sadsa|dd|g - g|hh|yyyyy - tttt - ooo');
insert into t values ('sadsa|dd|g - g|hh|yyyyy - tttt | - ooo');
insert into t values ('sad -sa|dd|g - g|hh|yy yyy tttt ooo');

select regexp_replace(c,'(.*)\|([^|-]*)-[^|]*','\1|\2-') from t;

gives

sadsa|dd|g - g|hh|yyyyy -
sadsa|dd|g - g|hh|yyyyy - tttt | -
sad -sa|dd|g -|hh|yy yyy tttt ooo

see

http://sqlfiddle.com/#!4/45eb2f/1