Go (RE2)

Git Remote URL (HTTPS or SSH) in GO

Match git remote URLs in both `git@host:org/repo` and `https://host/org/repo` forms.

Try it in the GO tester →

Pattern

regexGO
(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$`)
	input := `git@github.com:vercel/next.js.git`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

(?:git@|https?:\/\/) matches either the SSH `git@` prefix or HTTP/HTTPS scheme. ([\w.\-]+) captures the host. [:\/] matches the host/path separator (colon for SSH, slash for HTTPS). The next two groups capture org and repo. (?:\.git)? optionally strips the trailing `.git`. \/? allows a trailing slash.

Examples

Input

git@github.com:vercel/next.js.git

Matches

  • git@github.com:vercel/next.js.git

Input

https://gitlab.com/group/subproject

Matches

  • https://gitlab.com/group/subproject

Input

not a git url

No match

Same pattern, other engines

← Back to Git Remote URL (HTTPS or SSH) overview (all engines)