Pattern library / URLs & Marketing
GitHub URL Regex
https://github.com/adityaeza7-beep/regexlab — but also http://, www., deep links (/tree/main/src, ?tab=repos) and the SSH form your git config actually uses. This pattern matches the web forms, requires owner/repo, and the notes show the capture groups that split them apart.
^https?://(www\.)?github\.com/[A-Za-z0-9-]+/[A-Za-z0-9_.-]+([/?].*)?$Load this pattern into the tester →
How the pattern works
/after owner and repo is what separates a repository link from a profile (github.com/octocat) or a settings page.- Capture the pair:
^https?://(www\.)?github\.com/([^/]+)/([^/?#]+)— group 2 = owner, group 3 = repo (it stops before /tree/, issues or ?query). - Scheme optional for links found in the wild: start with
^(https?://)?(www\.)?github\.com/— keep it required when the input should be a full URL. - SSH remote (
git@github.com:user/repo.git):^git@github\.com:([^/]+)/([^/]+)(\.git)?$— a separate shape; do not try to merge it into the HTTP one.
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://github.com/adityaeza7-beep/regexlab | Match | canonical repository URL | Try → |
https://github.com/aloisdg/awesome-regex/tree/main/lists | Match | deep link with /tree/ | Try → |
https://github.com/user/repo?tab=repos | Match | query-only tail after the repo | Try → |
github.com/user/repo | No match | no scheme — optional variant in notes | Try → |
https://github.com/octocat | No match | owner without a repo segment | Try → |
git@github.com:user/repo.git | No match | SSH form — different shape entirely | Try → |
Where to use it
JavaScript (owner + repo)
const m = url.match(/^https?:\/\/(www\.)?github\.com\/([^/]+)\/([^/?#]+)/); // m[2] = owner, m[3] = repo
Python
import re m = re.match(r'^https?://(?:www\.)?github\.com/([^/]+)/([^/?#]+)', url) owner, repo = m.groups() if m else (None, None)
Bash (grep repo links)
grep -Eo '^https?://(www\.)?github\.com/[A-Za-z0-9-]+/[A-Za-z0-9_.-]+' urls.txt | head
Common mistakes
- The repo charset
[A-Za-z0-9_.-]also allows dotted segments likeuser/.— validate the captured pair against your rules if it gates access. github.ioPages andgist.github.comare different hosts — this pattern requiresgithub\.com/owner/repoexactly.- UTM junk (
?utm_source=github) is absorbed by the([/?].*)tail — strip query noise when you normalize links for storage or comparison.
FAQ
How do I extract the owner and repo name?
Use the capture variant from the notes: ^https?://(www\.)?github\.com/([^/]+)/([^/?#]+). Groups 2 and 3 are owner and repo — group 3 stops before /tree/, /issues or ?query.
Why does git@github.com:user/repo.git not match?
It is the SSH remote form, not a URL: no slashes after the host, colon instead. Test it with ^git@github\.com:([^/]+)/([^/]+)(\.git)?$ instead.
How do I match any github.com link, including profiles?
Drop the required repo part: ^https?://(www\.)?github\.com/[A-Za-z0-9-]+(/[A-Za-z0-9_.-]+)*(/.*)?$ — accept that it also matches 404-prone paths, then filter captures in code.
Related patterns
URL Regex — Match http/https Links (Practical) · YouTube URL Regex - watch, youtu.be & Shorts (Extract Video ID) · Extract Domain from URL — Regex Capture Group
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.