我需要从 github 链接获取用户 ID例:https://github.com/myName/blabla/ or https://github.com/myName/
https://github.com/myName输出应为 。myName我的代码:(\/github.com\/)\/(.*?)(\/)?
2 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
你在中间有一个额外的斜杠,你不需要在末尾使用斜杠,只需抓取不是用户名斜杠的字符:
/github\.com/([^/]+)
您的比赛将在第1组中。
交互式演示:https://regex101.com/r/vEOksV/2
凤凰求蛊
TA贡献1825条经验 获得超4个赞
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
此示例的交互式 IDE:https://onlinegdb.com/ry1w1dSB8
添加回答
举报
0/150
提交
取消
