import Foundation
let pattern = #"(?!letsgo\.|shop\.|advertise\.|static\.|www\.)((\w|-)+\.)(tumblr\.com|tmblr\.co)\/?(?!\S)"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .dotMatchesLineSeparators])
let testString = ##"""
Match the following:
https://dhabitahpunk-art.tumblr.com/
https://cdn.tumblr.com
DON'T match the following:
https://www.tumblr.com/todayontumblr
tumblr.com/todayontumblr
https://www.tumblr.com/todayontumblr/
tumblr.com/todayontumblr/
https://www.tumblr.com/dhabitahpunk-art
tumblr.com/dhabitahpunk-art
https://www.tumblr.com/dhabitahpunk-art/
tumblr.com/dhabitahpunk-art/
https://tmblr.co/MzB5rhaOrTp3uwB5NhcQsEw
https://www.tumblr.com/blog/view/grouper/
https://www.tumblr.com/internships
https://www.tumblr.com/transparency
https://www.tumblr.com/search/lana
https://www.tumblr.com/tagged/lwaxana%20troi?sort=top
https://www.tumblr.com/following
https://tumblr.com/customize/
https://letsgo.tumblr.com/
https://letsgo.tumblr.com/welcome-guide
https://64.media.tumblr.com/6c05087e5dbaccedf651d421148dfaa0/e7eb8d73a9a6cc70-8e/s540x810/e9cc402adacca3e983fc0448b94919b9f809c719.gifv
https://shop.tumblr.com/
https://shop.tumblr.com/new/
https://www.tumblr.com/tumblrmart/blue-checkmark
https://help.tumblr.com/hc/articles/115001572547
https://help.tumblr.com/hc
https://tumblr.com/help
https://api.tumblr.com/console
https://www.tumblr.com/oauth/apps
https://www.tumblr.com/api
https://assets.tumblr.com/downloads
https://www.tumblr.com/abuse
https://www.tumblr.com/themes/
https://www.tumblr.com/themes/tagged/two_column
https://www.tumblr.com/dmca
https://www.tumblr.com/docs/api_agreement
https://www.tumblr.com/account/delete
https://www.tumblr.com/settings
https://www.tumblr.com/settings/blog
https://www.tumblr.com/security
https://www.tumblr.com/developers
https://www.tumblr.com/press
https://www.tumblr.com/buttons
https://www.tumblr.com/logo
https://www.tumblr.com/tips
https://www.tumblr.com/support
https://www.tumblr.com/auth
https://www.tumblr.com/auth/google?redirectTo=undefined
https://www.tumblr.com/register
https://www.tumblr.com/register?source=new_to_tumblr
https://www.tumblr.com/login
https://www.tumblr.com/login?redirect_to=%2Fexplore%2Ftoday
https://www.tumblr.com/explore
https://www.tumblr.com/explore/trending
https://advertise.tumblr.com/
https://advertise.tumblr.com/#why
https://www.tumblr.com/jobs
https://www.tumblr.com/policy
https://www.tumblr.com/privacy_policy
https://www.tumblr.com/policy/privacy
https://www.tumblr.com/policy/terms-of-service
https://www.tumblr.com/apps
https://www.tumblr.com/about
https://about.tumblr.com/#quick-facts
https://www.tumblr.com/
https://www.tumblr.com/dhabitahpunk-art/tagged/Picturesque
tumblr.com/dhabitahpunk-art/tagged/Picturesque
tumblr.com
https://dhabitahpunk-art.tumblr.com/page/2
dhabitahpunk-art.tumblr.com/page
https://dhabitahpunk-art.tumblr.com/page/
https://dhabitahpunk-art.tumblr.com/commissions
https://dhabitahpunk-art.tumblr.com/post/714262490632044544/time-taken-355-hoursreference-unknown
https://static.tumblr.com/
https://static.tumblr.com/zyubucd/CIjopeea7/transparencyreport2016b.pdf
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression