Extract domain from text in Mandarin

From LemonWiki共筆
Jump to navigation Jump to search

從文章內容,擷取網址中的網域部分

🌐 Switch language: English, 漢字


使用 Google sheet 擷取網域[edit]

使用 Google 試算表 REGEXEXTRACT 函數

=REGEXEXTRACT(A1, "(http[s]?\://[^/]+)")

輸入:

Yahoo! 新聞 https://tw.news.yahoo.com/abc

輸出:

https://tw.news.yahoo.com

說明:

  1. 網域指以 http:// 或 https:// 開頭,與相臨不是符號 / 的多個文字:[^/]+


資料驗證:文章內容是否包含網域[edit]

原始資料包含網域,但是網域前面不包含 http e.g. tw.news.yahoo.com 或 www.bbc.co.uk。使用 Google 試算表 REGEXMATCH 函數,符合正規表示法的規則的話,回傳 TRUE。若不符合,則回傳 FALSE。 Icon_exclaim.gif 以下語法未處理 IPv4 形式的網域。(如果網域前面包含 http ,則可直接搜尋關鍵字: regular expression extract host )

=IF(ISERROR(REGEXMATCH(A1, "([a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+\.[a-zA-Z]{2,}$|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")), FALSE, REGEXMATCH(A1, "([a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+\.[a-zA-Z]{2,}$|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"))

輸入1:

www.bbc.co.uk

輸出1:

TRUE

輸入2:

127.0.0.0

輸出2:

TRUE

輸入3:

Yahoo! 新聞

輸出3:

FALSE

不建議的其他方法:

  • 檢查網域結尾是否是 .com, .tw, .net, .org 因為太多要列舉,該方法沒有效率。