正则表达式以匹配SSH url部分 [英] Regex to match SSH url parts

查看:311
本文介绍了正则表达式以匹配SSH url部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供以下SSH网址:

git@github.com:james/example
git@github.com:007/example
git@github.com:22/james/example
git@github.com:22/007/example

我该如何拉动以下内容:

How can I pull the following:

{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

谢谢 @Jan ,感谢您 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆