import Foundation
let pattern = #"iPhone [6-7]"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
iPhone 7 Plus
iPad Pro 12.9 2015 iOS15.5 beta 4
iPhone 11 Pro iOS15.5 beta4
iPad Pro 10.5 2017
iPad 9.7 2018 iOS15.5 beta4
iPhone X
iPhone 11 Pro Max
iPad Pro 10.5 2017
iPad mini 3
iPhone X
iPad Pro 12.9 2021
iPhone 13
iPhone 11
iPhone 12 mini
iPad Pro 12.9 2018
iPad Pro 11 2018
iPhone 12
iPhone X iOS15.5 beta4
iPad Air 2
iPhone 8
iPhone 6
iPhone XS Max
iPad mini 4
iPad Air 3 (2019)
iPhone SE 2020
iPhone 11 Pro
iPhone 12 Pro
iPad Mini 2019
iPad 9.7 2018
iPad 10.2 2020
iPhone SE
iPad Air 2
iPad Air 2022 5th Gen
iPad Air 2020
iPad Air 2
iPad Pro 11 2021
iPhone SE 2020
iPhone 6 Plus
iPad Pro 11 2020
iPad 10.2 2020
iPad Pro 10.5 2017
iPad 10.2
iPhone XR
iPad Pro 12.9 2015
iPad Pro
iPad Air 2
iPhone 7
iPad Mini 2021 6th Gen
iPad Air 2020
iPhone 8 Plus
iPhone 11
iPad 9.7 2017 5th Gen
iPhone XS
iPad Pro 12.9 2020
iPhone 6S
iPad Air
iPhone XS Max
iPhone SE 2020
iPhone_11_Pro
iPhone 5S
iPhone 8
iPhone 7 Plus
iPod Touch 6
iPad Pro 9.7
iPhone 12 mini iOS15.5 beta4
iPhone X
iPhone 8
iPhone 6
iPhone X
iPhone 13 Pro Max
iPhone 12 Pro Max
iPad Air 2 iOS15.5 beta4
iPhone 6
iPad mini 4
iPad Pro 11 2018
iPad Pro 12.9 2015
iPhone XR
iPhone XS
iPhone XR
iPhone 7
iPad Pro 12.9 2017
iPhone 8
iPhone 6S
iPad Pro 12.9 2017
iPad Pro 12.9
iPhone X
iPhone 8 Plus
iPad Air 3 (2019)
iPhone 8
iPhone 12 Pro Max
iPad Pro 12.9 2015
iPad mini 4
iPhone 13 mini
iPad Air 2
iPhone 13 Pro
iPhone 7
iPhone XS
iPad 10.2 2021 9th Gen
iPhone SE 2020
iPod Touch 7
iPad Air 2
iPhone 7 Plus
iPhone SE 2022
iPhone 6S Plus
iPhone 12
"""#
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