import Foundation
// WARNING: You included a flag that Swift doesn't support: D
// When this flag is set, it forces the a dollar sign ('$'), to always match end of the string, instead of end of the line. This option is ignored if the 'm' flag is set.
// There's no equivalent implementation of this flag in Swift, but you may want to check the 'm' flag instead.
let pattern = #".*(\w{4}\d{7}).*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
UNB+UNOA:2+RCW OPS CENTER+TERMINAL+180808:1519+1533741570C3ED+++++RCW OPS CENTER'UNH+01533741570BAP+BAPLIE:D:95B:UN:SMDG22'BGM++CAPSTAN4.20180808151930+9'DTM+137 1808081519UTC:301'TDT+20+081S+++HSD:172:166+++9V7575:103:ZZZ:xxxx xxx'LOC+5+BRSSA:139:6'LOC+61+COCTG:139:6'DTM+178:1808090412 :201'DTM+133:1808091512:201'DTM+132:1808180041:201'RFF+VON:081N'LOC+147+0380412::5'MEA+WT++KGM:29515'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOHAI:139:6'RFF+BM:1'EQD+CN+
SUDU8505087+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380312::5'MEA+WT++KGM:29586'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOCAU:139:6'RFF+BM:1'EQD+CN+
UACU5363691+45G1+++5'NAD+CA+HLC:172:20'LOC+147+0380212::5'MEA+WT++KGM:29591'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+COCTG:139:6'RFF+BM:1'EQD+CN+
TGHU9702812+45G1+++5'NAD+CA+MSC:172:20'LOC+147+0380112::5'MEA+WT++KGM:29616'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOCAU:139:6'RFF+BM:1'EQD+CN+
HLXU6240079+45G1+++5'NAD+CA+HLC:172:20'LOC+147+0380414::5'MEA+WT++KGM:29476'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+PRSJU:139:6'RFF+BM:1'EQD+CN+
HASU4556735+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380314::5'MEA+WT++KGM:29476'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+DOHAI:139:6'RFF+BM:1'EQD+CN+
SUDU6787839+45G1+++5'NAD+CA+HSD:172:20'LOC+147+0380214::5'MEA+WT++KGM:29481'LOC+9+BRSSA:139:6+TECSV'LOC+11+COCTG:139:6+TCC'LOC+83+COCTG:139:6'RFF+BM:1'EQD+CN+...
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1\r\n"#
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