简体   繁体   English

使用模式从字符串中提取子字符串

[英]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'模式: 'menu'

Substring looking for: 'xyz'子串寻找: 'xyz'

If your example accurately represents your end goal, the url_extract_parameter() function is likely a better solution than regex:如果您的示例准确地代表了您的最终目标,则url_extract_parameter()函数可能是比正则表达式更好的解决方案:

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

In Presto DB, you should be able to use function regexp_extract() , in the form that supports capturing groups:在 Presto DB 中,您应该能够以支持捕获组的形式使用函数regexp_extract()

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

This will do:这将:

menu=([^&]+)

You can check it here你可以在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM