git存储库的正则表达式

时间:2010-03-25 11:00:47

标签: regex

git存储库的正则表达式是什么?

示例链接: git@github.com:有人/ someproject.git

所以它会是这样的 [用户] @ [服务器]:[项目]的.git

服务器可以是url或ip 项目可以包含除“ - ”之类的字母数字之外的其他字符 我不确定'/'

的作用是什么

有什么建议吗?

9 个答案:

答案 0 :(得分:37)

我正在使用以下正则表达式用于在线远程存储库:

  

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

View on Debuggex

Regular expression visualization

答案 1 :(得分:10)

Git接受大量的存储库URL表达式:

* 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/

对于我编写的需要解析这些表达式(YonderGit)的应用程序,我提出了以下(Python)正则表达式:

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

对于“在野外”遇到的大多数存储库URL,我怀疑(1)就足够了。

答案 2 :(得分:2)

粗略

^[^@]+@[^:]+:[^/]+/[^.]+\.git$

答案 3 :(得分:1)

仅供参考我是从github或bitbucket获得所有者和回购的正则表达式:

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

Debuggex Demo

答案 4 :(得分:1)

在bash中,您可以不使用正则表达式来做到这一点:

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

输出:

exercises-php

答案 5 :(得分:0)

Git存储库可以有许多形状和大小,看起来就像那个例子。有关完整列表,请参见git-clone手册页。

一些较常见的包括使用httpgit协议而不是SSH(或者,实际上,手动指定ssh://协议)。用户名是可选的,不必是/.git,可以指定端口等等。

目前你基本上只允许私人Github回购,或者恰好看起来像他们的那些。那是你要的吗?如果是这样,S。Mark的回答看起来不错!

如果你想接受任何git存储库,最好的办法是确保它是一个有效的URI,然后使用git或git库来确保在该URI上有一个真正的repo可访问。

答案 6 :(得分:0)

((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})

这还将为您提供用户和存储库。

答案 7 :(得分:0)

尝试此正则表达式:

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

对我来说很好。

答案 8 :(得分:0)

# Sample JSON
$changeduserdata = '{"DeviceAddresses":[{"Id": 42,"More": "stuff"}]}'

# Note how [ and { are \-escaped
$deviceId = if ($changeduserdata -match 'DeviceAddresses":\[\{"Id":(.*?),"') {
  $Matches[1]
}

唯一的问题是不支持IP地址。

相关问题