import Foundation
let pattern = #"(?<!cpc|accounts\.)(google|bing|search\.yahoo|search\.myway|yandex|duckduckgo|ecosia|search\.tb\.ask|baidu)(?!.*cpc)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
google/cpc/[google][cpc][house][sale]
google/cpc/[google][cpc][condo][sale]
accounts.google.com.sg
accounts.google.co.id
google.com
google.com.ph
google.co.th
bing.com
google.co.uk
com.google.android.googlequicksearchbox
google.com.sg
google.com.au
google.com.vn
google.ca
google.co.id
google.com.hk
search.yahoo.com
duckduckgo.com
google.de
ph.search.yahoo.com
google.co.in
google.fr
google.co.jp
google.co.kr
google.com.my
google.ae
google.se
google.ch
int.search.myway.com
yandex.ru
google.ru
google.com.tw
google.nl
cn.bing.com
ecosia.org
google.it
google.dk
google.co.nz
int.search.tb.ask.com
search.yahoo.co.jp
google.co.za
google.no
google.es
google.be
th.search.yahoo.com
google.fi
baidu.com
google.ie
google.com.tr
uk.search.yahoo.com
"""#
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