快速替换多个字符
presto replace multiple characters
我有一个字符串,想删除一组字符。有没有比链接多个 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
这是一个最小的例子,我有更多的字符需要转义,所以我一直在寻找一种不太冗长的方法来实现这一点。我没有在文档中找到能够直接执行此操作的字符串函数。
谢谢。
您可以使用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)
我有一个字符串,想删除一组字符。有没有比链接多个 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
这是一个最小的例子,我有更多的字符需要转义,所以我一直在寻找一种不太冗长的方法来实现这一点。我没有在文档中找到能够直接执行此操作的字符串函数。
谢谢。
您可以使用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)