import Foundation
let pattern = #"(https?://(?![^\s\/]+youtu)[^\s\)\]?]+)\?([^\s\)\]?]{20,})"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"https://www.aliexpress.com/item/4000050337687.html?spm=a2g0o.productlist.0.0.70c9492bADfZA5&algo_pvid=66a91537-1872-4a3b-ab39-e9d130970423&algo_exp_id=66a91537-1872-4a3b-ab39-e9d130970423-0&pdp_ext_f=%7B%22sku_id%22%3A%2210000003069527876%22%7D&pdp_npi=2%40dis%21EUR%2118.03%2114.07%21%21%21%21%21%402100bdd816603176696127137ebeac%2110000003069527876%21sea&curPageLogUid=8f5jqvWtAYMa"#
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