0

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]" 

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Not working. This returns a single array. ["[a,b],[c,d],[e,f]"]
Got the exact output i needed using case 1.. Thanks much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.