-1
我想驗證Swift 3中的URL,但我似乎無法找到適合我需求的正則表達式。正則表達式,我需要後接受以下組合:在Swift 3中驗證URL
http://google.com
http://google.com/foo/bar:30/35
https://google.com
https://google.com/foo/bar:30/35
www.google.com
google.com
我想驗證Swift 3中的URL,但我似乎無法找到適合我需求的正則表達式。正則表達式,我需要後接受以下組合:在Swift 3中驗證URL
http://google.com
http://google.com/foo/bar:30/35
https://google.com
https://google.com/foo/bar:30/35
www.google.com
google.com
func validateUrl (urlString: NSString) -> Bool {
let urlRegEx = "((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?"
return NSPredicate(format: "SELF MATCHES %@", urlRegEx).evaluateWithObject(urlString)
}
這個工作。
這仍然不接受子域名。例如,google.basecamp.com會返回false。任何幫助? –
當我谷歌'在swift 3.0驗證網址'我似乎得到了很多結果。顯然,沒有一個是Swift 3特有的,但它們帶有你需要的正則表達式,應該很容易移植。例如。 http://stackoverflow.com/questions/29106005/url-validation-in-swift –
我不明白爲什麼問題已經downvotes –
這很容易谷歌能夠應該首先谷歌搜索的問題。那麼當你遇到一個特定的問題,或者所有可用的解決方案都不起作用時,*那麼可能值得問一個問題(提到你已經嘗試了很多。) –