I would like to extract the substring after the last occurrence of ref_button_id
value in the string, in this example, string 'ref_button_id=pivot-rows5&ref_button_id=hhh-rows&'
will return hhh-rows
I am using presto sql and my try was:
select reverse(split(split(reverse('ref_button_id=pivot-rows5&ref_button_id=hhh-rows&'), '=di_nottub_fer&')[1], '&')[2]) as ref_button_id
Which can get what I need but feel it's not ideal, is there a more formal workaround of this please?
You can try using regular expressions with regexp_extract_all
:
-- sample data
with dataset(str) as (
values ('ref_button_id=pivot-rows5&ref_button_id=hhh-rows&'),
('ref_button_id=hhh-rows&'),
('non-refs=123')
)
-- query
select try(reverse(regexp_extract_all(str, '(?<=ref_button_id=)[^&#]+'))[1])
from dataset;
Output:
_col0 |
---|
hhh-rows |
hhh-rows |