Search code examples
sqlamazon-athenaprestotrino

Extracting string value before bracket using regexp in Athena


I am trying to learn about regexp and I am trying to extract data that is before open bracket character using regexp_extract.

String value:

device
iphone-ios-cx 12.45.0(2144)

Expected output:

device
iphone-ios-cx 12.45.0

I am able to achieve this using split_part function.

select split_part(device, '(', 1)
FROM devices_tbl

I tried the same using regexp_replace and regexp_extract, both seem to give me incorrect results

select regexp_replace(device, '\\(.*\\)','')
FROM devices_tbl

How can I achieve the same using regex functions in Athena?


Solution

  • You got parenthesis escaping wrong, use single \:

    select regexp_replace('iphone-ios-cx 12.45.0(2144)', '\(.*\)','');
    

    Output:

             _col0         
    -----------------------
     iphone-ios-cx 12.45.0