Python 来自Github链接的Regex用户ID

Python 来自Github链接的Regex用户ID,python,regex,Python,Regex,我需要从github链接获取用户ID 例如: https://github.com/myName/blabla/ or https://github.com/myName/ https://github.com/myName 输出应该是myName 我的代码: (\/github.com\/)\/(.*?)(\/)? 你在中间有一个额外的斜杠,并且你不需要最后的斜杠,只要抓取不是用户名为斜杠的字符: /github\.com/([^/]+) 你的对手将在第一组 交互式演示: 交互式IDE

我需要从github链接获取用户ID

例如:

https://github.com/myName/blabla/ or 
https://github.com/myName/
https://github.com/myName
输出应该是
myName

我的代码:

(\/github.com\/)\/(.*?)(\/)?

你在中间有一个额外的斜杠,并且你不需要最后的斜杠,只要抓取不是用户名为斜杠的字符:

/github\.com/([^/]+)
你的对手将在第一组

交互式演示:


交互式IDE与此示例:

看起来不像python代码。这是不正确的-它不匹配OP的所有测试用例。OP在我回答问题后修改了测试用例。唯一的修改是将正则表达式中的最后一个斜杠设为可选。他们确实修改了测试用例(在您回答问题之前),但失败的测试用例位于原始的:
import re

text = 'https://github.com/testName/'

try:
    found = re.search('github\.com\/(.+?)\/', text).group(1)
except AttributeError:
    # pattern not found
    found = '' # apply your error handling

# found: testName