如何在 sql 语句中使用 Oracle regexp 仅删除尾随空格以及 chr(10) 和 chr(13)?
示例:
with txt as (select chr(10)||chr(10)||' Hey Bob '||chr(10)||chr(13) a from dual)
select a
,regexp_replace(a,chr(10)||'+|'||chr(13)||'+|'||chr(32)||'+$','')
,regexp_replace(a,'['||chr(10)||'+'||chr(13)||'+'||chr(32)||'+]$','')
from txt;
期望的结果:
' Hey Bob'
1. Leading and non-trailing spaces remain
2. Trailing spaces and eol characters removed
最佳答案
最好使用[[:space:]]
来捕获所有空白:
regexp_replace(a, '[[:space:]]+$', '')
但是,如果您明确只需要换行符 (10)、回车符 (13) 和空格 (32) 字符,您可以这样做:
regexp_replace(a, '[' || chr(10) || chr(13) || ' ]+$', '')
-- ^-- space character.
关于regex - Oracle 正则表达式尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27385994/