import Foundation
let pattern = #"https:\/\/www\.chytryhonza\.cz\/(hypoteka|americka-hypoteka|refinancovani-hypoteky).*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
https://www.chytryhonza.cz/hypotecni-kalkulacka?v=iTBDcj1Zi0eb4mSEPe3k3CNWhpqvaZmAyRTmCq%2Be1j085RJkWNTwLmXX8mRWLjvUDUxgpyVpx0VS0q08qi%2FTIvRBMeiXW4w9fK%2F8cQg4wXlsDubtLGXAdytQCNC2LTfF
https://www.chytryhonza.cz/hypotecni-kalkulacka?v=
https://www.chytryhonza.cz/srovnavac
https://www.chytryhonza.cz/srovnavac-zivotniho-pojisteni?v=dd
https://www.chytryhonza.cz/srovnavac-zivotniho-pojisteni?v=1qC3j5r%2BHim0K68IAcodRMrYv6ikx0NneJC39H2oOdl2H8QZ5DL9glGwRtkQSV%2BhVT0c%2B95YLTSmA4BDugvpUQkWIDUmTmv4rcg1t78EMid9jhDOoHh%2BXxRZAxYBGKkB
https://www.chytryhonza.cz/hypoteka
https://www.chytryhonza.cz/hypoteka#aca30160bd78b
https://www.chytryhonza.cz/americka-hypoteka
https://www.chytryhonza.cz/refinancovani-hypoteky
https://www.chytryhonza.cz/zivot
"""##
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