Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive regexp_replace failed to replace backslash

I have a table with a single column name_string, which contains backslash character. I wanted to remove the backslash character using regexp_replace, but it does not work.

Table:

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

Query:

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

returning

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

However, select regexp_replace('\"aaa\"', '\\"', '"') returns the correct result.

I am confused about why this may be the case. Could someone please shed light on this? Appreciate it!

like image 943
Julia Liu Avatar asked Aug 12 '26 05:08

Julia Liu


1 Answers

Use 4 backslashes:

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

Only backslash needs escaping. In Java and in regex the backslash has special meaning and needs escaping.

like image 194
leftjoin Avatar answered Aug 16 '26 02:08

leftjoin