快速替换多个字符

use*_*152 1 regex sql string replace presto

我有一个字符串,想删除一组字符。有没有比链接多个更好的方法replace()

我想出了以下内容,但需要replace()对每个角色进行调用:


WITH my_table(id, a_string) AS (
     VALUES
         (1, 'test{' || CHR(39) || '}xyz'),
         (2, 'x'),
         (3, '{y}'),
         (1, 'z'),
         (2, 'none'),
         (3, 'none' || CHR(39) || 'xyz')
)
SELECT replace(replace(replace(a_string,CHR(39)),'{'),'}') FROM my_table
Run Code Online (Sandbox Code Playgroud)

这是一个最小的例子,我有更多的字符需要转义,所以我正在寻找一种不太冗长的方法来实现这一点。我在文档中没有找到能够直接执行此操作的字符串函数。

谢谢。

Mar*_*rso 6

您可以使用regexp_replace删除多个字符:

presto> WITH my_table(id, a_string) AS (
     ->      VALUES
     ->          (1, 'test{' || CHR(39) || '}xyz'),
     ->          (2, 'x'),
     ->          (3, '{y}'),
     ->          (1, 'z'),
     ->          (2, 'none'),
     ->          (3, 'none' || CHR(39) || 'xyz')
     -> )
     -> SELECT regexp_replace(a_string, '[{}'']') FROM my_table;
  _col0
---------
 testxyz
 x
 y
 z
 none
 nonexyz
(6 rows)
Run Code Online (Sandbox Code Playgroud)