我有以下字符串,我想使用正则表达式提取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
如何解决此问题?
答案 0 :(得分: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
答案 1 :(得分:1)
欢迎光临!
此表达式可能会帮助您做到这一点:
(.*"service_code":")(.*?)(".*)
具有三个捕获组,只是为了易于调用。您可以从第二组$2
中获取所需数据。
如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。
您还可以在jex.im中可视化您的表达式:
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);