import Foundation
let pattern = #"\b[A-Z]{2}\b"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
EFS Web 2.1.17
Corresponding to
1 102742238 CN A 2012-10-17 ZTE USA Inc. US20130094411A1
3 20150318972 2015-11-05 Zhang et al.
2 20130128860 2013-05-23 Zhang
1 20130094411 2013-04-18 Zhang
EFS Web 2.1.17
Examiner Signature Date Considered
3rd Generation Partnership Project; Technical Specification Group Radio Access Network; Evolved Universal
1 Terrestrial Radio Access (E-UTRA); Physical channels and modulation (Release 12), 3GPP TS 36.211 V12.7.0
fONAR_ NAN 12R nanac
vs Corresponding to
4 2014110837 WO Al 2014-07-24 Fujitsu Ltd. et al. US20150318972A1
3 2014107012 WO Al 2014-07-10 LG Electronics Inc.
; Corresponding to
2 103120006 CN A 2013-05-22 ZTE Corporation US20130128860A1 A fg FG ddDF dFGg A1 AL Al
EFS Web 2.1.17
| |
pore !
"""#
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