【问题标题】:How to use PL/SQL- REGEXP_REPLACE to remove 0D from end of string?如何使用 PL/SQL-REGEXP_REPLACE 从字符串末尾删除 0D?
【发布时间】:2011-10-23 14:37:57
【问题描述】:
我想在 Oracle 10g 上的 PL/SQL 中使用 REGEXP_REPLACE 从字符串中删除尾随的 HEX-0D。我从this question 找到了正则表达式。
在 Perl 中是这样写的:
$output =~ tr/\x{d}\x{a}//d;
或
$output =~ s/\s+\z//;
如何将其转换为 PL/SQL?
【问题讨论】:
标签:
regex
oracle
plsql
oracle10g
【解决方案1】:
/* PL/SQL */
declare
in_str constant varchar2(30) := 'foo' || chr(13) || 'bar' || chr(13);
out_str varchar2(30);
begin
dbms_output.put_line('in_str = ' || utl_raw.cast_to_raw(in_str));
select regexp_replace(in_str, chr(13) || '$', '') into out_str from dual;
dbms_output.put_line('out_str = ' || utl_raw.cast_to_raw(out_str));
end;
/
/* SQL */
select utl_raw.cast_to_raw('foo' || chr(13) || 'bar' || chr(13)) as BEFORE from dual;
select utl_raw.cast_to_raw(
regexp_replace('foo' || chr(13) || 'bar' || chr(13), chr(13) || '$', '')
) as AFTER from dual;
/* OUTPUT */
Session altered.
in_str = 666F6F0D6261720D
out_str = 666F6F0D626172
PL/SQL procedure successfully completed.
BEFORE
------------------------------------------------------------------------------
666F6F0D6261720D
AFTER
------------------------------------------------------------------------------
666F6F0D626172
【解决方案2】:
你也有 RTRIM 功能
select dump(a), dump(rtrim(a,chr(13))) from (select 'test'||chr(13) a from dual);