Try this: http: // ([- \ w \.] +) + (: \ D +)? (/ ([\ w / _ \.] * (\? \ S +)?)?))
let pattern = "http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" var matches = [String]() do { let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions(rawValue: 0)) let nsstr = text as NSString let all = NSRange(location: 0, length: nsstr.length) regex.enumerateMatchesInString(text, options: NSMatchingOptions.init(rawValue: 0), range: all, usingBlock: { (result, flags, _) in matches.append(nsstr.substringWithRange(result!.range)) }) } catch { return [String]() } return matches
source share