import Foundation
let pattern = #"^(?!(?:\s*$|.*(?:Niveau|stime)))(.*$)([\s\S]*?)(\1\s*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
vide vide Time: stime 3:30 PM vide vide
NN NN NP stime LS NP NN NN
----------Niveau 1--------------
Time: | 0 | 263.0 | 266.0 | 0,0113
NP | 0 | 0.0 | 24885.0 | 1
3:30 | -0 | 104.0 | 120.0 | 0,1333
LS | -0 | 0.0 | 13134.0 | 1
PM | -1 | 134.0 | 238.0 | 0,437
NP | -1 | 0.0 | 24885.0 | 1
----------Niveau 2--------------
3:30 PM | -0 | 30.0 | 41.0 | 0,2683
3:30 NP | -0 | 133.0 | 55.0 | -1,4182
LS PM | -0 | 42.0 | 237.0 | 0,8228
LS NP | -0 | 0.0 | 2456.0 | 1
----------Niveau 3--------------
vide vide Time: stime 3:30 pm vide vide
NN NN NP stime LS NN NN NN
----------Niveau 1--------------
Time: | 0 | 263.0 | 266.0 | 0,0113
NP | 0 | 0.0 | 24885.0 | 1
3:30 | -0 | 104.0 | 120.0 | 0,1333
LS | -0 | 0.0 | 13134.0 | 1
pm | -1 | 38.0 | 54.0 | 0,2963
NN | -1 | 0.0 | 59511.0 | 1
----------Niveau 2--------------
3:30 pm | -0 | 9.0 | 9.0 | 0
3:30 NN | -0 | 36.0 | 24.0 | -0,5
LS pm | -0 | 22.0 | 52.0 | 0,5769
LS NN | -0 | 0.0 | 2658.0 | 1
----------Niveau 3--------------
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"\1\2"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
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