我需要在github url中包含一个用户名和令牌,以访问github上的私有回购。
访问后,我需要对其进行消毒以获得干净的版本。
输入模式为https://{username}:{token}@github.com/{repo_owner}/{repo-name}
我想要的输出模式是https://github.com/{repo_owner}/{repo-name}
例如:我得到这个
https://usernameabc:token1234@github.com/abc/easy-as-123
我想要这个
https://github.com/abc/easy-as-123
我如何在Python中做到这一点?我可以使用regex
我用的是什么
我正在使用这个
def sanitize_github_url(github_url_with_username_token):
github_url_with_username_token = github_url_with_username_token.lower()
index = github_url_with_username_token.find("github.com/", 0)
suffix = github_url_with_username_token[index:]
return f"https://{suffix}"
它对我的目的有效。有更好的方法吗?
我不希望在这种情况下使用正则表达式,而是使用url操作库,如furl
,
from furl import furl
url = furl("https://usernameabc:token1234@github.com/abc/easy-as-123")
url.password = None
url.username = None
print(str(url))
输出:
https://github.com/abc/easy-as-123
使用前后查找的正则表达式
raw = r'https://usernameabc:token1234@github.com/abc/easy-as-123'
re.sub("(?<=https://).*?(?=github.com)", "", raw)