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
How can I pull the following:
{user}@{host}:{optional port}{path (user/repo)}
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>[^\\].*)$
Not sure what else to try.
Lazy quantifiers to the rescue!
This seems to work well and satisfies the optional port:
^
(?P<user>.*?)@
(?P<host>.*?):
(?:(?P<port>.*?)/)?
(?P<path>.*?/.*?)
$
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
Thank you @Jan for the optimizations.