import Foundation
let pattern = #"^(?:ht|f)tps?://[^/]+/[^.]+\.([^?]+)\?*$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"http://u.example.something.com/abc=aaadddrBccc2SgFV000ALK2Qddd_;_vvv=Yu3DUzBHNlzcHIEY29sYMTEzZHQ5NjUzBHNlY9sbwNiZjEEdnRpZANRMU0wMV8xBHBvcwMx/CC=20/RE=MU0wMV8xBHBvcwMx/UU=10/ZZ=http://hhh.w.example.com/?uv=xxxxxx878787xxxxxxxxjhkjhkjhjkhADFADFS-&u=http://wwww.example.com/%2fuuu-lllllll%2sdfg-afd-adsfadf%2asdfaf000%3asdfasdfasfd%3dbing%26uasdfadf%3dcpc%26asdfadf%3d_ASDF234_ASDF%26asdfasF_asdfasdf%3d%252Bfasdfadsf%2520%252Basdf%2520%252Bdesign%26asdfasdf_aasdf%3dlf9McvAV%7cFADF%7c4063555565%7cpkw%7c%252Bfree%2520%252Bpcb%2520%252Basdf%7casdf%7asdf%7adf%7adf%7c/PO=0/UJ=iasdf7PASDFASDF-?p=this+is+an+example;"#
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