import Foundation
let pattern = #"(\[.*\]\(audio-1 .*\))|(\[.*\]\(img-1 .*\))|(\[\]\(video-1 .*\))"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
[null,null,null](img-1 'https://cdnapi.ergolab.cn/api/cdn/files/Teams/Picture/1/2670fff8-2119-c821-d2e6-3a04b72ac556/jsjuHxumPt4yKPRtVfR.png')
[null,null,null](img-1 'https://cdnapi.ergolab.cn/api/cdn/files/Teams/Picture/0/2670fff8-2119-c821-d2e6-3a04b72ac556/jsjuHxumPt4yLavx4gv.png')
[null,null,null](img-1 'https://cdnapi.ergolab.cn/api/cdn/files/MaterialLibrary/Picture/0/3761f21b-3016-dadd-6198-3a0569770f55/jsjuHxukkZDtF879nQB.jpg')
[](audio-1 'https://cdnapi.ergolab.cn/api/cdn/files/Teams/Audio/0/2670fff8-2119-c821-d2e6-3a04b72ac556/jsjuHxumPt4zfnkYB6m.ogg')
[](audio-1 'https://cdnapi.ergolab.cn/api/cdn/files/MaterialLibrary/Audio/0/44b29ef1-e567-e1a4-49c6-3a0545afd2f4/jsjuHidHpFagk8AbHt7.mp3')
[](video-1 'https://cdnapi.ergolab.cn/api/cdn/files/Teams/Video/0/2670fff8-2119-c821-d2e6-3a04b72ac556/jsjuHxumPt4zfnoXzSB.ogg')
[](video-1 'https://cdnapi.ergolab.cn/api/cdn/files/MaterialLibrary/Video/0/ac35f3ad-4cbb-da07-e510-39fd24ba71b5/jseEvLjEA4JXbybZQse.mp4')
"""#
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