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.
How the pattern works
^https?://— matches both http and https, and pins the URL to the very start of the string so a URL buried inside a sentence is ignored.[^/?#]+— the authority: host, port and any credentials, running up to the first slash, question mark or hash.(/[^?#]*)?— the capture group. It starts at the first/and stops at the first?or#, so the query string and fragment never leak in. It is optional, which is exactly whyhttps://example.comstill matches.- Empty group means the root:
path = m ? (m[1] || '/') : nullin JavaScript, andCOALESCE(NULLIF(REGEXP_EXTRACT(u, r'…'), ''), '/')in SQL.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
https://www.example.com/pricing?utm_source=nl | Match | captures /pricing — the ? stops the group | Try → |
http://blog.site.co.uk:8080/api/v2/items | Match | the port belongs to the authority; the path starts at the first / | Try → |
https://example.com | Match | matches with an empty group — the path is / | Try → |
https://example.com/docs/regex/?q=1#top | Match | captures /docs/regex/ — trailing slash kept, query and fragment dropped | Try → |
see https://example.com/blog for more | No match | ^ pins the URL to the start of the string (see pitfalls) | Try → |
ftp://files.example.com/pub/report.pdf | No match | scheme 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
- Anchored by design. With
^a URL sitting inside a log line or a sentence never matches — drop the anchor to search inside text (https?://[^/?#]+(/[^?#]*)?); the capture group stays group 1. - The usual first attempt is
https?://.*(/.*)or a greedy.*after the host — greedy runs to the end of the string and swallows the query and fragment with the path.[^?#]*is what stops it. /pricingand/pricing/are two different strings, so analytics counts them as two pages. Strip a trailing slash before grouping:path.replace(/\/$/, '')— and normalize the bare root to/.
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.
Related patterns
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.