import Foundation
let pattern = #"/*.mkv"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
folder.jpg
metadata
Stargate SG-1 S01E01-E02 Children of the Gods.mkv
Stargate SG-1 S01E03 The Enemy Within.mkv
Stargate SG-1 S01E04 Emancipation.mkv
Stargate SG-1 S01E05 The Broca Divide.mkv
Stargate SG-1 S01E06 The First Commandment.mkv
Stargate SG-1 S01E07 Cold Lazarus.mkv
Stargate SG-1 S01E08 The Nox.mkv
Stargate SG-1 S01E09 Brief Candle.mkv
Stargate SG-1 S01E10 Thor's Hammer.mkv
Stargate SG-1 S01E11 The Torment of Tantalus.mkv
Stargate SG-1 S01E12 Bloodlines.mkv
Stargate SG-1 S01E13 Fire and Water.mkv
Stargate SG-1 S01E14 Hathor.mkv
Stargate SG-1 S01E15 Singularity.mkv
Stargate SG-1 S01E16 Cor-Ai.mkv
Stargate SG-1 S01E17 Enigma.mkv
Stargate SG-1 S01E18 Solitudes.mkv
Stargate SG-1 S01E19 Tin Man.mkv
Stargate SG-1 S01E20 There But For the Grace of God.mkv
Stargate SG-1 S01E21 Politics (1).mkv
Stargate SG-1 S01E22 Within the Serpent's Grasp (2).mkv
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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