28

What will be proper regular expression for git repositories?

example link: [email protected]:someone/someproject.git

so it will be like [user]@[server]:[project].git

server can be url or ip Project can contain some other characters than alphanumeric like '-' I'm not sure what is the role of '/'

any suggestions?

1
  • I'm not convinced a regular expression is the best solution for parsing this syntax, although many would disagree. See blog.codinghorror.com/…
    – glyn
    Jul 12, 2016 at 16:00

10 Answers 10

58

I'm using the following regular expression for online remote repositories:

((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?

View on Debuggex

Regular expression visualization

5
  • 22
    I came here looking for a regex statement so I didn't have to spend time on it and I found the best regex debugger ever made. Thank you.
    – blockloop
    Mar 14, 2016 at 22:09
  • 2
    I agree about Debuggex. Unfortunately, group 4 in the above expression doesn't cope with users other than "git".
    – glyn
    Jul 12, 2016 at 15:57
  • 1
    tweak a little for javascript use: /((git|ssh|http(s)?)|(git@[\w.]+))(:(\/\/)?)([\w.@:/\-~]+)(\.git)(\/)?/
    – Alessia
    Jan 2, 2018 at 3:41
  • +1, but git clone user@server:project.git, for instance, won't be matched against the provided regex. So it seems it works fine with "github-like" URLs, but not against all possible Git URLs...
    – rsenna
    Sep 19, 2020 at 8:37
  • This regex doesn't account for git hosts with dashes in the domian name. For example, git@custom-gitlab:my-group/myrepo.git. This regex bit me when trying to run with a custom-hosted gitlab instance. To correct, just add the dash in the domain portion: ((git|ssh|http(s)?)|(git@[\w\.-]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?
    – Joe J
    Oct 31, 2022 at 21:21
16

Git accepts a large range of repository URL expressions:

* ssh://[email protected]:port/path/to/repo.git/
* ssh://[email protected]/path/to/repo.git/
* ssh://host.xz:port/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://[email protected]/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://[email protected]/~user/path/to/repo.git/
* ssh://host.xz/~user/path/to/repo.git/
* ssh://[email protected]/~/path/to/repo.git
* ssh://host.xz/~/path/to/repo.git
* [email protected]:/path/to/repo.git/
* host.xz:/path/to/repo.git/
* [email protected]:~user/path/to/repo.git/
* host.xz:~user/path/to/repo.git/
* [email protected]:path/to/repo.git
* host.xz:path/to/repo.git
* rsync://host.xz/path/to/repo.git/
* git://host.xz/path/to/repo.git/
* git://host.xz/~user/path/to/repo.git/
* http://host.xz/path/to/repo.git/
* https://host.xz/path/to/repo.git/
* /path/to/repo.git/
* path/to/repo.git/
* ~/path/to/repo.git
* file:///path/to/repo.git/
* file://~/path/to/repo.git/

For an application that I wrote that requires parsing of these expressions (YonderGit), I came up with the following (Python) regular expressions:

    (1) '(\w+://)(.+@)*([\w\d\.]+)(:[\d]+){0,1}/*(.*)'
    (2) 'file://(.*)'       
    (3) '(.+@)*([\w\d\.]+):(.*)'

For most repository URL's encountered "in the wild", I suspect (1) suffices.

2
  • 2
    This does not work with a url like [email protected]:group/project.git
    – Anonymous
    Oct 29, 2014 at 11:07
  • Anonymous seems to be wrong, "git@" is just an instance of "[some-user]@", so it is matched by (3).
    – rsenna
    Sep 19, 2020 at 8:31
4

FYI I make a regex for get owner and repo from github or bitbucket:

(?P<host>(git@|https://)([\w\.@]+)(/|:))(?P<owner>[\w,\-,\_]+)/(?P<repo>[\w,\-,\_]+)(.git){0,1}((/){0,1})

Debuggex Demo

2

In bash, you can do this without regex:

basename https://github.com/code-co-ua/exercises-php

Output:

exercises-php
1

Roughly

^[^@]+@[^:]+:[^/]+/[^.]+\.git$
1
  • This does not handle Gerrit's git uri's. Feb 7, 2017 at 7:34
1
((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})

This will also give you user and repo in a seperate group.

0

Git repositories can come in many shapes and sizes that look nothing like that example. See the git-clone man page for a full list.

Some of the more common ones include using the http or git protocols instead of SSH (or, indeed, manually specifying the ssh:// protocol). Usernames are optional, there doesn't have to be a / or a .git, ports may be specified, etc etc.

At the moment you're basically only allowing private Github repos, or ones which happen to look like them. Is that what you want? If so, S. Mark's answer looks good!

If you want to accept any git repository, the best bet is probably to make sure it's a valid URI, and then use git or a git library to make sure there is a real repo accessible at that URI.

0

Try this regex:

/^([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+(:\d+)?)(?::|\/)([\d\/\w.-]+?)(\.git)?$/i

It works fine for me.

0
export const REGEXP_GIT_REPO_URI = new RegExp(/^(((https?\:\/\/)(((([a-zA-Z0-9][a-zA-Z0-9\-\_]{1,252})\.){1,8}[a-zA-Z]{2,63})\/))|((ssh\:\/\/)?git\@)(((([a-zA-Z0-9][a-zA-Z0-9\-\_]{1,252})\.){1,8}[a-zA-Z]{2,63})(\:)))([a-zA-Z0-9][a-zA-Z0-9\_\-]{1,36})(\/)([a-zA-Z0-9][a-zA-Z0-9\_\-]{1,36})((\.git)?)$/);

The only problem is not support IP address.

0

I saw the top comment with regex like this

((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?

but it does not pass this case in Golang: "[email protected]/abc.gits"

Thus for golang, you should use this regex:

((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.