Advertisement

Pattern library / Extract & Replace

Regex to Extract the Path from a URL

page_location is a whole URL, page_path is only the part after the host — that split is the single most common reason a GA4 page path filter returns nothing. Skip the scheme and authority, then take everything up to the first ? or #, and you have the path with no URL parser. Valid RE2, so the same pattern runs in GA4, BigQuery, Go and Rust.

The pattern
^https?://[^/?#]+(/[^?#]*)?
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
https://www.example.com/pricing?utm_source=nlMatchcaptures /pricing — the ? stops the groupTry →
http://blog.site.co.uk:8080/api/v2/itemsMatchthe port belongs to the authority; the path starts at the first /Try →
https://example.comMatchmatches with an empty group — the path is /Try →
https://example.com/docs/regex/?q=1#topMatchcaptures /docs/regex/ — trailing slash kept, query and fragment droppedTry →
see https://example.com/blog for moreNo match^ pins the URL to the start of the string (see pitfalls)Try →
ftp://files.example.com/pub/report.pdfNo matchscheme limited to http(s)Try →

Where to use it

JavaScript

const m = url.match(/^https?:\/\/[^/?#]+(\/[^?#]*)?/);
const path = m && (m[1] || '/');   // '/pricing'  |  '/' for a bare host

Python

import re
m = re.search(r'^https?://[^/?#]+(/[^?#]*)?', 'http://blog.site.co.uk:8080/x')
path = (m.group(1) or '/') if m else None   # '/x'

BigQuery / GA4 (RE2)

SELECT COALESCE(NULLIF(REGEXP_EXTRACT(page_location, r'^https?://[^/?#]+(/[^?#]*)?'), ''), '/') AS path,
       COUNT(*) AS views
FROM events GROUP BY 1 ORDER BY 2 DESC

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I keep the query string and drop only the fragment?

Loosen the class so it stops at the hash alone: ^https?://[^/?#]+([^#]*). Group 1 becomes /pricing?utm_source=nl instead of /pricing.

What about URLs with no scheme, like example.com/pricing?

Make the scheme optional: ^(https?://)?[^/?#]+(/[^?#]*)? — group 1 is now the scheme, group 2 the path. Fine for one-off cleanup; if you need to validate the host too, parse with new URL() or urllib.parse rather than stretching the regex.

Can I use this inside Google Analytics 4?

It is plain RE2, so it runs in BigQuery (REGEXP_EXTRACT) and in GA4's RE2 engine — but GA4 filters only match, they never return a substring. Extract paths in BigQuery or your ETL and compare against the result; the match type still decides whether it has to cover the whole value.

Extract Domain from URL — Regex Capture Group · Remove Query Parameters with Regex (Replace Example) · URL Regex — Match http/https Links (Practical)

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.