Warm tip: This article is reproduced from stackoverflow.com, please click
git regex

Regular expression for git repository

发布于 2020-03-27 15:44:10

What will be proper regular expression for git repositories?

example link: git@github.com: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?

Questioner
dfens
Viewed
112
Jeet 2010-03-25 19:24

Git accepts a large range of repository URL expressions:

* ssh://user@host.xz:port/path/to/repo.git/
* ssh://user@host.xz/path/to/repo.git/
* ssh://host.xz:port/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://user@host.xz/path/to/repo.git/
* ssh://host.xz/path/to/repo.git/
* ssh://user@host.xz/~user/path/to/repo.git/
* ssh://host.xz/~user/path/to/repo.git/
* ssh://user@host.xz/~/path/to/repo.git
* ssh://host.xz/~/path/to/repo.git
* user@host.xz:/path/to/repo.git/
* host.xz:/path/to/repo.git/
* user@host.xz:~user/path/to/repo.git/
* host.xz:~user/path/to/repo.git/
* user@host.xz: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.