Extract substring from a string using pattern

I would like to extract a substring from a lengthy string based on a pattern. Wondering what is the best way to get it?

http://abcdef?menu=xyz&source=push&push_id=1212239617294503480&message_id=7658a0a6-9d31-4c3c-9aa0-9169f24e2fdc

Pattern: 'menu'

Substring looking for: 'xyz'

728x90

3 Answers Extract substring from a string using pattern

This will do:

menu=([^&]+)

You can check it here

6 days ago

In Presto DB, you should be able to use function regexp_extract(), in the form that supports capturing groups:

REGEXP_EXTRACT(val, 'menu=([^&]+)', 1)

Regex breakdown:

menu=        # litteral string 'menu='
(            # beginning of capturing group number 1
  [^&]+          # at least one character other than '&'
)            # end of capturing group number 1

6 days ago

If your example accurately represents your end goal, the url_extract_parameter() function is likely a better solution than regex:

SELECT url_extract_parameter('http://abcdef?menu=xyz&source=push', 'menu');
-- returns 'xyz'

3 days ago