我想比較 flutter 中的兩個 url 鏈接,看看它們是否相似,然后回傳一個真值。一個例子是比較以下鏈接
https://stackoverflow.com/
https://stackoverflow.com/questions/ask
uj5u.com熱心網友回復:
我會這樣做:
var link1 = 'https://stackoverflow.com/';
var link2 = 'https://stackoverflow.com/questions/ask';
print(link2.contains(link1)); => it will return true
uj5u.com熱心網友回復:
此stackoverflow 答案顯示了用于域名匹配的正則運算式。你可以這樣使用它:
final domainRegex = RegExp(r"^(?:https?:\/\/)?(?:[^@\n] @)?(?:www\.)?([^:\/\n?] )");
var firstUrl = 'https://stackoverflow.com/';
var secondUrl = 'https://stackoverflow.com/questions/ask';
var firstDomain = domainRegex.firstMatch(first_url)?.group(1); //stackoverflow.com
var secondDomain = domainRegex.firstMatch(second_url)?.group(1); //stackoverflow.com
print(firstDomain == secondDomain); //returns true
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364258.html
上一篇:Python正則運算式匹配URL
