0

I am trying to validate an URL in Swift 3 but I can't seem to find the Regex that suits my needs. Regex that I am after needs to accept following combinations:

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
Eric Aya
  • 68,765
  • 33
  • 165
  • 232
Vinod Sobale
  • 832
  • 11
  • 17
  • 4
    When I Google `Validate URL in swift 3.0` I seem to get a lot of results. None, apparently, are Swift 3 specific, but they come with the regex you need and should be easy enough to port. E.g. http://stackoverflow.com/questions/29106005/url-validation-in-swift – Pekka Oct 12 '16 at 10:59
  • I don't see why question has downvotes – Vinod Sobale Oct 12 '16 at 11:42
  • 1
    Questions that are this easily Google-able should really be Googled first. When then you encounter a specific problem, or all of the available solutions don’t work, *then* it may be worth asking a question (mentioning what you’ve tried that far.) – Pekka Oct 12 '16 at 12:06
  • 3
    @Pekka웃 That's against the idea of the site. If there is a duplicate answer, sure, link to that. But just because something is Google-able doesn't mean it is not a valid question. Many Google results hit stackoverflow.com, which would not be the case had someone not asked here. – sdasdadas Oct 12 '16 at 15:51
  • using regex is not the best way to validate a URL. The answers for this question offer better alternatives https://stackoverflow.com/questions/28079123/how-to-check-validity-of-url-in-swift – djruss70 Jan 15 '18 at 02:56
  • one of the millions: https://www.regextester.com/94502 – it is hard to find a validator online, indeed. – holex Jan 18 '18 at 13:50
  • @sdasdadas Still, one of the reasons specifically mentioned when you hover over downvote is "no research" – klutt Jan 18 '18 at 21:43
  • Possible duplicate of [What is the best regular expression to check if a string is a valid URL?](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Ouroborus Jan 18 '18 at 21:46
  • 1
    Fyi, I stumbled across this answer while Googling for a solution to this precise problem and I'm still wondering what's the best way to validate a URL in Swift. Regex is not Swift specific, so this question is still relevant - even if the answer is that there's no Swift-specific mechanism to validate URLs. – diegoreymendez Apr 23 '19 at 14:21

1 Answers1

5
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)
}

This worked.

Vinod Sobale
  • 832
  • 11
  • 17
  • 2
    This still doesn't accept subdomains. For example, google.basecamp.com would return false. Any help? – Vinod Sobale Oct 17 '16 at 12:07
  • 1
    this regex covers most of the cases "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/?)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))*(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])*)" – Gowrie Sammandhamoorthy Jul 11 '18 at 00:03