import Foundation
let pattern = #"\s{1,}\S{1,}$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
ITGIS.COMP_OP_STN cos
ITGIS.NS_DIVISIONS nsd
ITGIS.COMP_OP_STN cos
ITGIS.NSPD_RESP_ZONE nrz
ITGIS.COMP_OP_STN cos
PUBDATA.US_COUNTIES usc
ITGIS.COMP_OP_STN cos
PUBDATA.US_CITIES_BOUNDARIES uscb
ITGIS.COMP_OP_STN cos
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
NFSAPP.CONTROL nfsac
PUBDATA.US_CITIES c
ENGXING.CI_LOCATIONANDCLASSIFICATION cils
ITGIS.NS_DIVISIONS nsd
ENGXING.CI_LOCATIONANDCLASSIFICATION cils
ITGIS.NSPD_RESP_ZONE nrz
ENGXING.CI_LOCATIONANDCLASSIFICATION cils
PUBDATA.US_COUNTIES usc
ENGXING.CI_LOCATIONANDCLASSIFICATION cils
PUBDATA.US_CITIES_BOUNDARIES uscb
ENGXING.DOTLIST dl
INNER JOIN ENGXING.CI_LOCATIONANDCLASSIFICATION cils
ENGXING.INVENTORY_QUE q
nsdMatches nsd
ENGXING.CI_OPERATINGRAILROAD cior
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
ENG.ENG_CROSSING ec
NFSAPP.CONTROL nfsac
PUBDATA.US_CITIES c
EGIS.ENG_MPMARK mp
ITGIS.NS_DIVISIONS nsd
EGIS.ENG_MPMARK mp
ITGIS.NSPD_RESP_ZONE nrz
EGIS.ENG_MPMARK mp
PUBDATA.US_COUNTIES usc
EGIS.ENG_MPMARK mp
PUBDATA.US_CITIES_BOUNDARIES uscb
EGIS.ENG_MPMARK mp
nrzMatches nrz
nsdMatches nsd
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
NFSAPP.CONTROL nfsac
PUBDATA.US_CITIES c
EGIS.ENG_SWITCH s
ITGIS.NS_DIVISIONS nsd
EGIS.ENG_SWITCH s
ITGIS.NSPD_RESP_ZONE nrz
EGIS.ENG_SWITCH s
PUBDATA.US_COUNTIES usc
EGIS.ENG_SWITCH s
PUBDATA.US_CITIES_BOUNDARIES uscb
EGIS.ENG_SWITCH s
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
NFSAPP.CONTROL nfsac
PUBDATA.US_CITIES c
EGIS.ENG_TOWER t
ITGIS.NS_DIVISIONS nsd
EGIS.ENG_TOWER t
ITGIS.NSPD_RESP_ZONE nrz
EGIS.ENG_TOWER t
PUBDATA.US_COUNTIES usc
EGIS.ENG_TOWER t
PUBDATA.US_CITIES_BOUNDARIES uscb
EGIS.ENG_TOWER t
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
PUBDATA.US_CITIES c
ITGIS.COMP_AEI_SCNR s
ITGIS.NS_DIVISIONS nsd
ITGIS.COMP_AEI_SCNR s
ITGIS.NSPD_RESP_ZONE nrz
ITGIS.COMP_AEI_SCNR s
PUBDATA.US_COUNTIES usc
ITGIS.COMP_AEI_SCNR s
PUBDATA.US_CITIES_BOUNDARIES uscb
ITGIS.COMP_AEI_SCNR s
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
NFSAPP.CONTROL nfsac
PUBDATA.US_CITIES c
ITGIS.NSPD_LOCATIONS l
ITGIS.NS_DIVISIONS nsd
ITGIS.NSPD_LOCATIONS l
ITGIS.NSPD_RESP_ZONE nrz
ITGIS.NSPD_LOCATIONS l
PUBDATA.US_COUNTIES usc
ITGIS.NSPD_LOCATIONS l
PUBDATA.US_CITIES_BOUNDARIES uscb
ITGIS.NSPD_LOCATIONS l
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
PUBDATA.US_CITIES c
NSPD.NSPD_POI poi
ITGIS.NS_DIVISIONS nsd
NSPD.NSPD_POI poi
ITGIS.NSPD_RESP_ZONE nrz
NSPD.NSPD_POI poi
PUBDATA.US_COUNTIES usc
NSPD.NSPD_POI poi
PUBDATA.US_CITIES_BOUNDARIES uscb
NSPD.NSPD_POI poi
nsdMatches nsd
nrzMatches nrz
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
PUBDATA.US_CITIES c
ITGIS.ITGIS_MP100S mp
ITGIS.NS_DIVISIONS nsd
ITGIS.ITGIS_MP100S mp
ITGIS.NSPD_RESP_ZONE nrz
ITGIS.ITGIS_MP100S mp
PUBDATA.US_COUNTIES usc
ITGIS.ITGIS_MP100S mp
PUBDATA.US_CITIES_BOUNDARIES uscb
ITGIS.ITGIS_MP100S mp
NFSAPP.CONTROL nfsac
nrzMatches nrz
nsdMatches nsd
cityBoundaryMatches
countyMatches
ITGIS.NSPD_LOCATIONS nl
ITGIS.NS_DIVISIONS nd
PUBDATA.US_CITIES c
"""#
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