配置单元regexp\u replace无法替换反斜杠

wecizke3  于 2021-06-25  发布在  Hive
关注(0)|答案(2)|浏览(395)

我有一张单列表 name_string ,其中包含反斜杠字符。我想用 regexp_replace ,但它不起作用。
表格:

create table t (name_string varchar(100));
insert into table t values ('\\"aaa\\"'), ('\\"bbb\\"');

查询:

select 
   name_string, regexp_replace(name_string, '\\"', '"')
from  t;

返回

+--------------+----------+
| name_string  |   _c1    |
+--------------+----------+
| \"aaa\"      | \"aaa\"  |
| \"bbb\"      | \"bbb\"  |
+--------------+----------+

然而, select regexp_replace('\"aaa\"', '\\"', '"') 返回正确的结果。
我不明白为什么会这样。有人能解释一下吗?谢谢你!

b4qexyjb

b4qexyjb1#

或许可以试试:

select 
   name_string, regexp_replace(name_string, '\\\"', '"')
from  t;

我认为这是关于转义-你转义2个字符-反斜杠和双引号

slwdgvem

slwdgvem2#

使用4个反斜杠:

select regexp_replace(name_string,'\\\\"','"') from t;

只有反斜杠需要转义。在java和regex中,反斜杠有特殊的含义,需要转义。

相关问题