3 回答

TA貢獻2019條經驗 獲得超9個贊
嘗試使用
import re
s = "https://myhost.mydomain.com/pnLVyL7HjrxMlxjBQkhcOMr2WUs=/400x400/https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png"
m = re.search(r"https://.+(https.+)$", s)
if m:
print(m.group(1))
輸出:
https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png

TA貢獻1906條經驗 獲得超10個贊
我建議采用這種方法:
https?(?!.*https?):\/\/.*\bmydomain\.(?:com|io).*
此正則表達式使用負向前查找來確保我們匹配的 URL 是輸入字符串中的最后一個。示例腳本:
inp = "https://myhost.mydomain.com/pnLVyL7HjrxMlxjBQkhcOMr2WUs=/400x400/https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png"
url = re.findall(r'https?(?!.*https?):\/\/.*\bmydomain\.(?:com|io).*', inp)[0]
print(url)
這打印:
https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png

TA貢獻1809條經驗 獲得超8個贊
由于有 2 個鏈接,您可以匹配第一個鏈接并捕獲組 1 中的第二個鏈接。
https?://myhost\.mydomain\.(?:com|io)/\S*?(https?://myhost\.mydomain\.(?:com|io)/\S*\.(?:jpe?g|png))
https?://myhost\.mydomain\.(?:com|io)/
匹配第一個鏈接的開頭\S*?
匹配 0+ 次非空白字符非貪婪(
捕獲組 1https?://myhost\.mydomain\.(?:com|io)/
匹配第二個鏈接的開頭\S*
匹配 0+ 次非空白字符\.(?:jpe?g|png)
匹配 .jpg 或 .jpeg 或 .png)
關閉組 1
例如
import re
regex = r"https?://myhost\.mydomain\.(?:com|io)/\S*?(https?://myhost\.mydomain\.(?:com|io)/\S*\.(?:jpe?g|png))"
test_str = ("https://myhost.mydomain.com/pnLVyL7HjrxMlxjBQkhcOMr2WUs=/400x400/https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png")
matches = re.search(regex, test_str)
if matches:
print(matches.group(1))
輸出
https://myhost.mydomain.com/images/98f9a734-52e2-4616-adf7-bf0165bbf738.png
添加回答
舉報