import Foundation
let pattern = #"^(\d{2}:\d{2}:\d{2}[.,]\d{3})\s-->\s(\d{2}:\d{2}:\d{2}[.,]\d{3})\n(.*(?:\r?\n(?!\r?\n).*)*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
WEBVTT
1
00:00:00.310 --> 00:00:02.540
Dans cette séquence, nous
allons voir pourquoi nos
2
00:00:02.550 --> 00:00:05.210
assistants personnels,
smartphones et tablettes, sont
3
00:00:05.210 --> 00:00:07.420
devenus tout
naturellement, en une dizaine d'années
4
00:00:07.420 --> 00:00:10.100
seulement, un point de collecte
majeur de données personnelles.
5
00:00:10.520 --> 00:00:13.140
Pourquoi le smartphone
intéresse-t-il tant de monde ?
6
00:00:15.690 --> 00:00:17.550
Tout d'abord, il faut
être conscient que dans un
7
00:00:17.550 --> 00:00:20.110
smartphone, 2 niveaux
existent. Il y a la partie visible
8
00:00:20.150 --> 00:00:23.440
avec son processeur dont on
va vanter les performances,
9
00:00:23.440 --> 00:00:26.260
le système d'exploitation
- dans ce module, nous nous
"""#
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