正则表达式匹配 SSH url 部分 [英] Regex to match SSH url parts
本文介绍了正则表达式匹配 SSH url 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定以下 SSH 网址:
Given the following SSH urls:
git@github.com:james/example
git@github.com:007/example
git@github.com:22/james/example
git@github.com:22/007/example
我如何提取以下内容:
{user}@{host}:{optional port}{path (user/repo)}
正如您在示例中看到的,其中一个用户名是数字而不是端口.我不知道如何解决这个问题.端口也不总是在 URL 中.
As you can see in the example, one of the usernames is numeric and NOT a port. I can't figure out how to workaround that. A port isn't always in the URL too.
我当前的正则表达式是:
My current regex is:
^(?P<user>[^@]+)@(?P<host>[^:s]+)?:(?:(?P<port>d{1,5})/)?(?P<path>[^\].*)$
不知道还能尝试什么.
推荐答案
懒惰的量词来救援!
这似乎运行良好并且满足可选端口:
This seems to work well and satisfies the optional port:
^
(?P<user>.*?)@
(?P<host>.*?):
(?:(?P<port>.*?)/)?
(?P<path>.*?/.*?)
$
换行符不是正则表达式的一部分,因为启用了 /x
修饰符.如果您不使用 /x
,请删除所有换行符.
The line breaks are not part of the regex because the /x
modifier is enabled. Remove all line breaks if you are not using /x
.
https://regex101.com/r/wdE30O/5
这篇关于正则表达式匹配 SSH url 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文