RegEx用于从URL提取值

时间:2019-05-17 18:02:25

标签: regex google-bigquery regex-lookarounds regex-group regex-greedy

我有以下字符串,我想使用正则表达式提取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

如何解决此问题?

2 个答案:

答案 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中获取所需数据。

Demo

enter image description here

RegEx

如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here

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);