nani
nani

Reputation: 165

hive - Regex in Split function not giving output

Input :

[a,b], [c,d], [e,f]
select split(col,'\\,') from table_1; 

With the above query, am able to split on every comma. (inside and outside the braces)

I need to split only on the commas outside the braces. so i changed the query as below.

select split(col,',(?=\[)') from table_1;

regex which i used is correct as for my knowledge but am not able to get output.

Output required:

"[a,b]","[c,d]","[e,f]" 

Upvotes: 0

Views: 818

Answers (1)

Ashrith
Ashrith

Reputation: 6855

It looks like there is a space in between your data, so try this regex instead:

,\\s(?=\\[)

EDIT:

So, I am not sure if you have spaces or not in the column, so here is for both:

case 1: without spaces in the column

hive> describe a;
OK
t                       string

hive> select * from a;
OK
[a,b],[c,d],[e,f]
Time taken: 0.089 seconds, Fetched: 1 row(s)

hive> select split(t, ',(?=\\[)') from a;
OK
["[a,b]","[c,d]","[e,f]"]
Time taken: 0.081 seconds, Fetched: 1 row(s)

case 2: with spaces in the column

hive> describe b;
OK
t                       string              

hive> select * from b;
OK
[a,b], [c,d], [e,f]
Time taken: 0.084 seconds, Fetched: 1 row(s)

hive> select split(t, ',\\s(?=\\[)') from b;
OK
["[a,b]","[c,d]","[e,f]"]
Time taken: 0.082 seconds, Fetched: 1 row(s)

Upvotes: 1

Related Questions