import Foundation
let pattern = #"(?<!\d{8})_(?!\d{6})"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Patate_17890505_TitreEnCamelCase.ext
EPFL_AlgebreLineaire
TaxonomieProprietesFonctionsComplexes.v0.ipe.20210302_005606.pdf
1_
12_
_1
_12
12345678_
_123456
12345678_12345
1234567_123456
1234567_12345
123456_12345
12345678_1234567
123456789_123456
123456789_1234567
_patate__truc__
___
foo_12345678
foo_12345678_123456_bar
12345678_123456
foo12345678_123456bar
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"."#
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