Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive: regexp_replace square brackets

I need to replace the square brackets around a key:value pair similar to the following. Any help is much appreciated!

data in 'properties' looks like this:

name: property1
value: [12345667:97764458]

**code**
SELECT p.name, regexp_replace(p.value,'[','') AS value
FROM properties p

Solved: Revised code

SELECT p.name, regexp_replace(p.value,'\\[|\\]','') AS value
FROM properties p;
like image 207
Sarah Bergquist Avatar asked Jan 15 '14 02:01

Sarah Bergquist


People also ask

How to use regexp_replace function in hive?

Regexp_replace function in Hive. We need to define the pattern of the string like java regular expression in the Regexp_replace function.If the given string pattern match with the input string, it will replace all the occurrence of string to new string that given as a string replacement value in the function.

How to replace a string with another string in hive?

Hive provides few functions to handle the string replacement. Using string replace functions , we can replace some characters or the string with another character or string in Hive table values. Lets see the following string functions in detail. regexp_replace ; translate ; Regexp_replace function in Hive

How do you use brackets in regular expressions in Oracle?

A bracket expression [\ [\] which matches either a \ character or a [ character or a \ character (since \ is not an escape character in Oracle's regular expression syntax but is treated as a character literal). Followed by a ] character. So your regular expression would match a sub-string that was either \] or [].

What is the difference between regexp_replace () and re regexp_extract () functions?

Regexp_replace function replaces the string that matches a regular expression pattern with another string. Regexp_extract function extracts a portion of the string that matches a regular expression pattern with another string. RLIKE function to evaluate the LIKE operation using regular expression pattern.


2 Answers

You always need to double your backslashes in Hive regexes. That's because a single backslash is used as an escape character in Hive strings so it would get stripped off before the regex parser can see it. A double backslash becomes a single backslash, which is what you need.

To see how backslashes get stripped just run a select using your regex as a string literal:

select '\\[' from t limit 1;
OK
\[
like image 177
iggy Avatar answered Sep 21 '22 17:09

iggy


Here is you regex [\[\]]+ this will match one or more [ and ] in a string.

like image 28
João Pinho Avatar answered Sep 21 '22 17:09

João Pinho