Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to get the string inside parentheses using hive sql

Tags:

regex

sql

hive

I am trying to get the string between parentheses but i am getting always getting empty value.

String_Input: select sum(OUTPUT_VALUE) from table_name
Output : OUTPUT_VALUE

What I tried here:

select regexp_extract(String_Input,"/\\(([^)]+)\\)/") from table_name;

any suggestion to get the value ?

like image 285
tkumaran Avatar asked Apr 30 '26 23:04

tkumaran


1 Answers

If you need to get the value without the parentheses, you should indicate that you need to extract Captturing group 1 value in the third argument to regexp_extract function. Besides, you should remove / delimiters, they are parsed as literal symbols.

select regexp_extract(String_Input,"\\(([^)]+)\\)", 1) from table_name;
                                    ^            ^  ^ 

From the Hive documentation:

The 'index' parameter is the Java regex Matcher group() method index. See docs/api/java/util/regex/Matcher.html for more information on the 'index' or Java regex group() method.

like image 67
Wiktor Stribiżew Avatar answered May 03 '26 13:05

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!