import Foundation
let pattern = #"(?<=\'\),')(.*)(?=\',)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
DECODE(TRUE,
IN(LTRIM(RTRIM(deposit_account_type__)),'410'),'Brokered',
IN(LTRIM(RTRIM(deposit_account_type__)),'411'),'CDS Listing Service',
IN(LTRIM(RTRIM(deposit_account_type__)),'408'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'412'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'418'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'424'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'436'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'448'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'460'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'472'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'484'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'496'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'497'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'708'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'712'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'718'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'724'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'736'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'748'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'760'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'772'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'784'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'796'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'797'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'400'),'Public Funds CD',
IN(LTRIM(RTRIM(deposit_account_type__)),'700'),'Public Funds CD',
IN(LTRIM(RTRIM(deposit_account_type__)),'407'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'432'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'482'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'491'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'707'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'732'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'782'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'791'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'100'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'101'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'102'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'110'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'111'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'115'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'125'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'127'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'130'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'149'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'150'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'151'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'314'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'320'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'345'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'380'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'381'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'382'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'386'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'387'),'Checking',
IN(LTRIM(RTRIM(deposit_account_type__)),'330'),'HSA',
IN(LTRIM(RTRIM(deposit_account_type__)),'331'),'HSA',
IN(LTRIM(RTRIM(deposit_account_type__)),'161'),'Money Management',
IN(LTRIM(RTRIM(deposit_account_type__)),'351'),'Money Management',
IN(LTRIM(RTRIM(deposit_account_type__)),'375'),'Portfolio Investment',
IN(LTRIM(RTRIM(deposit_account_type__)),'376'),'Portfolio Investment',
IN(LTRIM(RTRIM(deposit_account_type__)),'377'),'Portfolio Investment',
IN(LTRIM(RTRIM(deposit_account_type__)),'385'),'Portfolio Investment',
IN(LTRIM(RTRIM(deposit_account_type__)),'303'),'Sweep LOC',
IN(LTRIM(RTRIM(deposit_account_type__)),'841'),'Sweep LOC',
IN(LTRIM(RTRIM(deposit_account_type__)),'842'),'Sweep LOC',
IN(LTRIM(RTRIM(deposit_account_type__)),'843'),'Sweep LOC',
IN(LTRIM(RTRIM(deposit_account_type__)),'508'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'510'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'512'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'518'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'524'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'536'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'540'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'548'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'560'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'572'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'584'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'596'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'597'),'Long Term - 1 Year or More',
IN(LTRIM(RTRIM(deposit_account_type__)),'599'),'Savings',
IN(LTRIM(RTRIM(deposit_account_type__)),'503'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'507'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'532'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'582'),'Short Term - Less than 1 Year',
IN(LTRIM(RTRIM(deposit_account_type__)),'851'),'Holiday',
IN(LTRIM(RTRIM(deposit_account_type__)),'820'),'Savings',
IN(LTRIM(RTRIM(deposit_account_type__)),'825'),'Savings',
IN(LTRIM(RTRIM(deposit_account_type__)),'830'),'Savings',
IN(LTRIM(RTRIM(deposit_account_type__)),'845'),'Savings',
IN(LTRIM(RTRIM(deposit_account_type__)),'887'),'Savings'
)
"""#
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