import Foundation
let pattern = #"(https?:\/\/)?(v|player)\.youku\.com\/(v_show|player\.php|embed)(\/.*sid)?\/(id_)?(\w+=*)|(\w+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
http://v.youku.com/v_show/id_XMTMwMDYxMjQxMg==_ev_1.html?from=y1.3-idx-uhome-1519-20887.205805-205902.1-1
http://v.youku.com/v_show/id_XMjMxOTQyOTQw.html?from=y1.6-97.3.1.a44aa406e0c711df97c0
http://v.youku.com/v_show/id_XMTI5Mjg5NjE4MA==.html?from=y1.3-idx-uhome-1519-20887.205921-205922-205810-205923.1-1
http://v.youku.com/v_show/id_XMTI5Mjg5NjE4MA==.html
http://player.youku.com/player.php/sid/XMTI5Mjg5NjE4MA==/v.swf
http://player.youku.com/player.php/sid/XMTI5Mjg5NjE4MA==/v.swf
http://player.youku.com/embed/XMTI5Mjg5NjE4MA==
http://player.youku.com/player.php/Type/Folder/Fid/25924643/Ob/1/sid/XMTMwMDgxNTY0NA==/v.swf
http://player.youku.com/embed/XMTI5NTcwMDA3Mg==
"""#
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