Social
GitHub repository URL Regex
A GitHub repository URL has two path segments: the owner, which follows the username rules, and the repository name, which may contain letters, digits, dots, dashes and underscores. This pattern checks that structure with optional https and www, and accepts a trailing .git suffix or slash so clone URLs and browser links both pass. It is handy for project submission forms, dependency manifests and README link checkers. Deeper paths such as issues, pull requests or file views are rejected on purpose. It does not support SSH clone URLs like git@github.com:owner/repo.git, and it cannot tell whether the repository is public.
Pattern (JavaScript)
/^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$/Starts at the start of the line/string, then (the text "http", then the character "s", optionally (zero or one time), then the text "://"), optionally (zero or one time), then (the text "www."), optionally (zero or one time), then the text "github.com/", then a letter or digit, then (the character "-", optionally (zero or one time), then a letter or digit), between 0 and 38 times, then the character "/", then one of the characters A-Za-z0-9._-, one or more times, as few as possible (lazy), then (the text ".git"), optionally (zero or one time), then the character "/", optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ https://github.com/test-user/sample-repo
- ✓ github.com/octo-test/regex.lab.git
- ✓ https://www.github.com/test-org/demo_project/
Does not match
- ✗ https://github.com/test-user
- ✗ https://gitlab.com/test-user/sample-repo
- ✗ https://github.com/test-user/sample-repo/issues/1
- ✗ https://github.com/-bad/repo
Try it
0 matches (JavaScript engine)
https://github.com/test-user/sample-repo
github.com/octo-test/regex.lab.git
https://www.github.com/test-org/demo_project/
https://github.com/test-user
https://gitlab.com/test-user/sample-repo
https://github.com/test-user/sample-repo/issues/1
https://github.com/-bad/repoCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:https?:\/\/)?(?:www\.)?github\.com\/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}\/[A-Za-z0-9._-]+?(?:\.git)?\/?$/g;
const text = "https://github.com/test-user/sample-repo";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$')
text = 'https://github.com/test-user/sample-repo'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$#';
$text = 'https://github.com/test-user/sample-repo';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:https?://)?(?:www\\.)?github\\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\\.git)?/?$");
String text = "https://github.com/test-user/sample-repo";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$`)
text := "https://github.com/test-user/sample-repo"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$");
var text = "https://github.com/test-user/sample-repo";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$").unwrap();
let text = "https://github.com/test-user/sample-repo";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:https?:\/\/)?(?:www\.)?github\.com\/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}\/[A-Za-z0-9._-]+?(?:\.git)?\/?$/
text = 'https://github.com/test-user/sample-repo'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I capture the owner and repository name?
- Wrap the owner segment and the repository segment in capture groups. Because the repository part is lazy, a trailing .git stays outside the group, so you get the clean name.
- Why are issue and pull request links rejected?
- Those links point to a page inside a repository rather than the repository itself. If you want to accept them, add an optional (?:/.*)? before the end anchor.
- Can I match SSH clone URLs too?
- Add an alternative for git@github\.com: followed by the same owner and repository segments. SSH URLs use a colon instead of a slash after the host, so they need their own branch.
Related patterns
- Discord invite link^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$
- Facebook page URL^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$
- GitHub username^(?=.{1,39}$)[A-Za-z0-9](?:-?[A-Za-z0-9])*$
- LinkedIn profile URL^(?:https?://)?(?:[a-z]{2,3}\.)?linkedin\.com/in/[A-Za-z0-9-]{3,100}/?$