我需要從 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
提交
取消