import Foundation
let pattern = #"^http[s]?:\/\/[^\/]*\.[a-z]+\/?$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
https://www.feetrader.com/jobPostings
http://www.bienesonline.es/asociarse/
http://www.bienesonline.mx/asociarse/
http://www.bienesonline.cl/asociarse/
http://www.bienesonline.co/asociarse/
http://www.geralimoveis.com.br/produtos/
http://usa.bienesonline.com/asociarse/
http://www.informatik.uni-linz.ac.at/jobs/
http://informatik.jku.at/jobs/
https://2012.nosql-matters.org/bcn/
https://www.mapmeo.com/partner
http://www.revistapremier.com.br/contemporanea/
https://zippyapp.com/business
http://graph.storn.es/
http://www.volcanicinternet.com/portfolio/
http://imobsync.com.br/
http://trovit.com.isdownorblocked.com/
http://infoday.hoyu.edu.hk/test/
http://2016.desymfony.com/patrocinadores
https://osclass.com/en/
https://imobtic.com.br/
http://www.trovit.at-s.seohighscore.com/
https://dom.yuga.ru/sites/
https://www.kadaza.com/classifieds
http://www.oxleyrobert.com/
http://blog.trovit.de/
http://job.trovit.at/
https://www.sistemarsi.com/privacy
https://www.docjobs.at/
https://www.bienesonline.com.ar/asociarse/
"""#
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