import Foundation
let pattern = #"\b540[^$](\s+\d{3,})"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
090323 0014 0006 0 506 120
090323 0020 0008 0 100 506
090323 0121 0001 0 506 540
090323 0851 0002 0 550 315
090323 0858 0004 0 600 540
090323 0900 0009 0 100 170
090323 0906 0002 0 500 530
090323 0912 0005 9 888 6999 989888888888 7 001 0
090323 0913 0006 9 888 140 989888888888 7 001 0
090323 0922 0002 0 550 205
090323 0927 0005 9 888 6999 989888888888 7 001 0
090323 0928 0012 9 888 140 989888888888 7 001 0
090323 0930 0004 0 550 120
090323 0931 0021 9 888 120 989888888888 7 001 0
090323 0937 0023 7 9 888 89888888888 150 7 010 4
090323 0937 0005 9 888 6999 989888888888 7 001 0
090323 0937 0000 0 100 130
090323 0939 0022 9 888 140 989888888888 7 001 0
090323 0939 0002 0 100 602
090323 0942 0013 9 888 120 989888888888 7 001 0
090323 0944 0009 9 888 120 989888888888 7 001 0
090323 0944 0003 9 888 130 989888888888 7 001 0
090323 0952 0007 9 888 6999 989888888888 7 001 0
090323 1002 0041 9 888 120 989888888888 7 001 0
090323 1005 0004 0 506 100
090323 1013 0002 0 100 413
090323 1015 0000 9 888 6999 989888888888 7 001 0
090323 1017 0004 9 888 6999 989888888888 7 001 0
090323 1018 0010 9 888 120 989888888888 7 001 0
090323 1024 0001 0 130 140
090323 1025 0004 0 500 130
090323 1026 0005 0 530 500
090323 1027 0036 7 9 888 89888888888 150 7 012 4
090323 1028 0005 7 9 888 89888888888 150 7 013 4
090323 1030 0019 9 888 120 989888888888 7 001 0
090323 1037 0001 0 540 506
090323 1037 0002 0 600 506
090323 1041 0002 A 9 888 89888888888 173 3 016 4
090323 1043 0052 9 888 120 988128888885 7 001 0
090323 1043 0002 0 600 540
090323 1045 0001 9 888 6999 989888888888 7 001 0
090323 1045 0001 9 888 540 989888888888 7 001 0
090323 1047 0005 7 888 5651 524 7 017 4
090323 1047 0005 9 9 888 888 89888888888 9840 7 001 018 0
090323 1048 0004 0 500 530
090323 1106 0001 0 600 506
090323 1118 0001 9 888 6999 989888888888 7 001 0
090323 1118 0005 9 888 120 989888888888 7 001 0
090323 1123 0003 0 540 506
090323 1126 0001 7 9 888 89888888888 140 7 019 4
090323 1127 0003 9 888 6999 988188888888 7 001 0
090323 1128 0009 9 888 120 988188888888 7 001 0
090323 1135 0003 0 533 100
090323 1144 0004 0 533 100
090323 1145 0000 7 9 888 89888888888 544 7 020 4
090323 1146 0003 0 130 316
090323 1147 0004 9 888 6999 989888888888 7 001 0
090323 1147 0002 9 888 140 989888888888 7 001 0
090323 1148 0002 0 550 120
090323 1149 0007 7 9 888 89888888888 544 7 022 4
090323 1149 0000 7 9 888 89888888888 514 7 023 4
090323 1151 0006 9 888 6999 989888888888 7 001 0
090323 1155 0043 9 888 120 989888888888 7 001 0
090323 1157 0001 9 888 6999 989888888888 7 001 0
090323 1158 0001 0 130 140
090323 1158 0004 9 888 6999 989888888888 7 001 0
090323 1159 0014 9 888 140 989888888888 7 001 0
090323 1202 0005 0 100 314
090323 1214 0002 0 100 310
090323 1218 0005 7 9 888 89888888888 172 3 024 4
090323 1222 0001 0 540 600
090323 1222 0007 7 9 888 89888888888 172 3 025 4
090323 1225 0010 9 888 120 989888888888 7 001 0
090323 1228 0002 0 130 509
090323 1229 0016 9 888 120 989888888888 7 001 0
090323 1234 0003 0 100 544
090323 1241 0015 7 9 888 89888888888 508 7 026 4
090323 1242 0005 9 888 6999 989888888888 7 001 0
090323 1243 0011 9 888 140 989888888888 7 001 0
090323 1245 0018 0 100 208
"""#
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