git git存储库的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2514859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression for git repository
提问by dfens
What will be proper regular expression for git repositories?
git 存储库的正确正则表达式是什么?
example link: [email protected]:someone/someproject.git
示例链接:[email protected]:someone/someproject.git
so it will be like [user]@[server]:[project].git
所以它会像 [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 '/'
服务器可以是 url 或 ip 项目可以包含一些其他字符而不是字母数字,如“-”我不确定“/”的作用是什么
any suggestions?
有什么建议?
采纳答案by Jeet
Git accepts a large range of repository URL expressions:
Git 接受大量的存储库 URL 表达式:
* 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:
对于我编写的需要解析这些表达式 ( YonderGit)的应用程序,我想出了以下 (Python) 正则表达式:
(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.
对于“在野外”遇到的大多数存储库 URL,我怀疑 (1) 就足够了。
回答by Kel Solaar
I'm using the following regular expression for online remote repositories:
我对在线远程存储库使用以下正则表达式:
((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?
((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?
回答by YOU
Roughly
大致
^[^@]+@[^:]+:[^/]+/[^.]+\.git$
回答by moylop260
FYI I make a regex for get owner and repo from github or bitbucket:
仅供参考,我为从 github 或 bitbucket 获取所有者和回购做了一个正则表达式:
(?P<host>(git@|https://)([\w\.@]+)(/|:))(?P<owner>[\w,\-,\_]+)/(?P<repo>[\w,\-,\_]+)(.git){0,1}((/){0,1})
回答by Ashish Pandey
((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})
This will also give you user and repo in a seperate group.
这也将为您提供单独组中的用户和存储库。
回答by Ostap B.
In bash, you can do this without regex:
在 bash 中,您可以在没有正则表达式的情况下执行此操作:
basename https://github.com/code-co-ua/exercises-php
Output:
输出:
exercises-php
回答by ?убиша Иванови?
Try this regex:
试试这个正则表达式:
/^([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+(:\d+)?)(?::|\/)([\d\/\w.-]+?)(\.git)?$/i
It works fine for me.
这对我来说可以。
回答by Chris
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.
Git 存储库可以有多种形状和大小,与该示例完全不同。有关git-clone
完整列表,请参阅手册页。
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.
一些更常见的包括使用http
或git
协议而不是 SSH(或者,实际上,手动指定ssh://
协议)。用户名是可选的,不必是 a/
或 a .git
,可以指定端口等。
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!
目前,您基本上只允许私有 Github 存储库,或者碰巧看起来像它们的存储库。那是你要的吗?如果是这样,S. Mark 的回答看起来不错!
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.
如果你想接受任何 git 存储库,最好的办法可能是确保它是一个有效的 URI,然后使用git
git 库来确保在该 URI 上有一个真正的 repo 可访问。