【问题标题】:RegEx for extracting a value from URLs用于从 URL 中提取值的正则表达式
【发布时间】:2019-05-17 18:02:25
【问题描述】:
我有以下字符串,我想使用正则表达式提取 POOL。
/costs/quotes/questions?api_key=b03f8da1faaf643806b1282c0e1177a0c54f3bc7&funnel=12&buttons=btn-dark-orange&category=22&zip_code=76102&phone=888-668-8262&step=1&step1_title=Where can we contact you?&source_position=landing-page-top-start-here-its-free&attributes={"category":"22","service_code":"POOL"}&ref=www.xyz.com/cost-pool-builders-fort-worth-tx
我该如何解决这个问题?
【问题讨论】:
标签:
regex
google-bigquery
regex-lookarounds
regex-group
regex-greedy
【解决方案1】:
欢迎!
这个表达式可能会帮助你做到这一点:
(.*"service_code":")(.*?)(".*)
它有三个捕获组,只是为了简单调用。你可以从第二组$2获取你想要的数据。
正则表达式
如果这不是您想要的表达方式,您可以在regex101.com 中修改/更改您的表达方式。
正则表达式电路
您还可以在jex.im 中可视化您的表达式:
JavaScript 演示
const regex = /(.*"service_code":")(.*?)(".*)/gm;
const str = `/costs/quotes/questions?api_key=b03f8da1faaf643806b1282c0e1177a0c54f3bc7&funnel=12&buttons=btn-dark-orange&category=22&zip_code=76102&phone=888-668-8262&step=1&step1_title=Where can we contact you?&source_position=landing-page-top-start-here-its-free&attributes={"category":"22","service_code":"POOL"}&ref=www.xyz.com/cost-pool-builders-fort-worth-tx`;
const subst = `$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
【解决方案2】:
以下 BigQuery 标准 SQL 示例
#standardSQL
WITH `project.dataset.table` AS (
SELECT '/costs/quotes/questions?api_key=b03f8da1faaf643806b1282c0e1177a0c54f3bc7&funnel=12&buttons=btn-dark-orange&category=22&zip_code=76102&phone=888-668-8262&step=1&step1_title=Where can we contact you?&source_position=landing-page-top-start-here-its-free&attributes={"category":"22","service_code":"POOL"}&ref=www.xyz.com/cost-pool-builders-fort-worth-tx' col
)
SELECT REGEXP_EXTRACT(col, r'&attributes={.*?"service_code":"(.*?)"') AS service_code
FROM `project.dataset.table`
结果
Row service_code
1 POOL