2

I am using dataset of github to extract all paths after /api/* and for that I used this below-mentioned query. However, the results are not what I expected it to be. If the regex is wrong can someone please correct it?

Expected results:

  • /api/v1/user

  • /api/anything/anything

What actually returns:

  • Frameworks/TwitterKit.framework/Resources
  • doc/source/README.rst
  *
FROM 
   `bigquery-public-data.github_repos.files`
WHERE
(REGEXP_CONTAINS(path,r'(s|^.*/api/([^/]*)(?:/.*)?$|$1|)'))
LIMIT
100```

3
  • Try REGEXP_CONTAINS(path, r'^/api/[^/]*(?:/.*)?$') Sep 21, 2020 at 12:06
  • @WiktorStribiżew thanks for the answer but now the query results as "This query returned no results."
    – nab_learn
    Sep 21, 2020 at 12:22
  • Well, the regex is working... Sep 21, 2020 at 12:28

2 Answers 2

1

You are using a Perl substitution command in the regex pattern. Look:

s|^.*/api/([^/]*)(?:/.*)?$|$1 |
||                        |RHS
||___pattern______________|
|___ action

where RHS (right-hand side) is the replacement.

You only need to use a pattern in BigQuery. To match your desired strings, you may use

^/api/[^/]*(?:/.*)?$

See the RE2 regex demo.

SELECT * FROM `bigquery-public-data.github_repos.files`
  WHERE REGEXP_CONTAINS(path,r'^/api/[^/]*(?:/.*)?$')
    LIMIT 100
1

If the regex is wrong can someone please correct it?

#standardSQL
SELECT *
FROM `bigquery-public-data.github_repos.files`
WHERE REGEXP_CONTAINS(path, r'/api/.*')
LIMIT 100   

Meantime, note: title of your question is not consistent with question body - REGEXP_CONTAINS in WHERE clause just allows you to return all rows with searched pattern in path - but does not extract the pattern.

To extract pattern - you need to use REGEXP_EXTRACT(path, r'/api/.*') in SELECT statement.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.