RegEx для извлечения значения из URL - PullRequest
0 голосов
/ 17 мая 2019

У меня есть следующая строка, я хотел бы извлечь 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 ]

2 голосов
/ 17 мая 2019

Ниже приведен пример для BigQuery Standard 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 голос
/ 17 мая 2019

Добро пожаловать!

Это выражение может помочь вам сделать это:

(.*"service_code":")(.*?)(".*)

, у которого есть три группы захвата, просто для простоты вызова. Вы можете получить нужные данные из второй группы $2.

Демо

enter image description here

RegEx

Если это не было вашим желаемым выражением, вы можете изменить / изменить выражения в regex101.com .

RegEx Circuit

Вы также можете визуализировать свои выражения в 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);
...