import Foundation
let pattern = ##"(?:http|https):\/\/((?:[\w-]+)(?:\.[\w-]+)+)(?:[\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
https://developer.mozilla.org/es/
http://www.es.pornhub.com/
http://es.cumlouder.com/?nats=MTkzOC4xLjIuMi4wLjAuMC4wLjA
http://www.pornotube.com/orientation/straight/home/page/1
http://rubias19.com/
http://www.elreyx.com/
http://www.puritanas.com/
http://www.xvideos.com/
http://es.xhamster.com/
http://www.redtube.com/
http://es.cam4.com/
https://es.chaturbate.com/
http://new.bangbros.com/home.htm
http://www.brazzers.com/home/?nats=NDc1NzAzOjQ5MzoyNA,0,0,0,0
http://www.petardas.com/
http://www.poringa.net/
http://foxtube.com/
http://www.tubewolf.com/
http://www.youporn.com/
http://www.putalocura.com/
http://www.serviporno.com/
http://www.toropornohd.com/
http://www.aztepajas.com/
http://pornfun.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