import Foundation
let pattern = #"""
(?m)
(?:
^ \d{2} - \d{4} [^|\r\n]* \|
| \G
)
(?: [^|\r\n]* \| )*
\K
[\r\n]+ (?! [\r\n]* (?: ^ \d{2} - \d{4} | $ ) )
"""#
let regex = try! NSRegularExpression(pattern: pattern, options: .allowCommentsAndWhitespace)
let testString = #"""
00-1234T|
Data|Commments|
12-3456|Some data|Notes|
65-8436ZZ|Data|
|
45-4576AA|Some data|Comments|
98-4392REV|Data|
|
00-5432|Some Data|Some Comments|
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = ##"#CRLF#"##
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