import Foundation
let pattern = #"^.+[\n\r]+.+[\n\r]+"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Market:;XFRA
Date Last Update:;23.04.2024
Product Status;Instrument Status;Instrument;ISIN;Product ID;Instrument ID;WKN;Mnemonic;MIC Code;CCP eligible Code;Trading Model Type;Product Assignment Group;Product Assignment Group Description;Designated Sponsor Member ID;Designated Sponsor;Price Range Value;Price Range Percentage;Minimum Quote Size;Instrument Type;Tick Size 1;Upper Price Limit Max;Tick Size 2;Upper Price Limit 2;Tick Size 3;Upper Price Limit 3;Tick Size 4;Upper Price Limit 4;Tick Size 5;Upper Price Limit 5;Tick Size 6;Upper Price Limit 6;Tick Size 7;Upper Price Limit 7;Tick Size 8;Upper Price Limit 8;Tick Size 9;Upper Price Limit 9;Tick Size 10;Upper Price Limit 10;Tick Size 11;Upper Price Limit 11;Tick Size 12;Upper Price Limit 12;Tick Size 13;Upper Price Limit 13;Tick Size 14;Upper Price Limit 14;Tick Size 15;Upper Price Limit 15;Tick Size 16;Upper Price Limit 16;Tick Size 17;Upper Price Limit 17;Tick Size 18;Upper Price Limit 18;Tick Size 19;Upper Price Limit 19;Tick Size 20;Upper Price Limit 20;Number of Decimal Digits;Unit of Quotation;Market Segment;Market Segment Supplement;Clearing Location;Primary Market MIC Code;Reporting Market;Settlement Period;Settlement Currency;Closed Book Indicator;Market Imbalance Indicator;CUM/EX Indicator;Minimum Iceberg Total Volume;Minimum Iceberg Display Volume;EMDI Incremental A - Unnetted;EMDI Incremental A - Unnetted Port;EMDI Incremental B - Unnetted;EMDI Incremental B - Unnetted Port;EMDI Snapshot A - Unnetted;EMDI Snapshot A - Unnetted Port;EMDI Snapshot B - Unnetted;EMDI Snapshot B - Unnetted Port;EMDI Market Depth - Unnetted;EMDI Snapshot Recovery Time Interval - Unnetted;MDI Address A - Netted;MDI Port A - Netted;MDI Address B - Netted;MDI Port B - Netted;MDI Market Depth - Netted;MDI Market Depth Time Interval - Netted;MDI Recovery Time Interval - Netted;EOBI Incremental A;EOBI Incremental Port A;EOBI Incremental B;EOBI Incremental Port B;EOBI Snapshot A;EOBI Snapshot Port A;EOBI Snapshot B;EOBI Snapshot Port B;Market Maker Member ID;Market Maker;Regulatory Liquid Instrument;Pre-trade LIS Value;Partition ID;Multi CCP-eligible;Tick Size Band;Security Sub Type;Issue Date;Underlying;Maturity Date;Flat Indicator;Coupon Rate;Previous Coupon Payment Date;Next Coupon Payment Date;Pool Factor;Indexation Coefficient;Accrued Interest Calculation Method;Country Of Issue;Minimum Tradable Unit;In-Subscription;Strike Price;Minimum Order Quantity;Off-Book Reporting Market;Instrument Auction Type;Specialist Member ID;Specialist;Liquidity Provider User Group;Specialist User Group;Quoting Period Start;Quoting Period End;Currency;Warrant Type;First Trading Date;Last Trading Date;Deposit Type;Single Sided Quote Support;Liquidity Class;Cover Indicator;VolatilityCorridorOpeningAuction;VolatilityCorridorIntradayAuction;VolatilityCorridorClosingAuction;VolatilityCorridorContinuous;DisableOnBookTrading
Active;Active;RAIF.BK INT. 18/25FLR MTN;AT000B014345;13;10583;000A1915H;;XFRA;N;ContinuousAuctionSpecialist;PAG_BON;BOERSE FRANKFURT BONDS;;;;;1;BOND;0.001;9999999999.9999;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;5;Percent;085;;BOFRA;WBAH;FRAB;2;EUR;1;0;;;;224.0.160.109;56001;224.0.162.109;56001;224.0.160.108;56000;224.0.162.108;56000;1;500000;224.0.161.46;56000;224.0.163.46;56000;1;3500;300000;224.0.160.237;56001;224.0.162.237;56001;224.0.160.236;56000;224.0.162.236;56000;BALFR;BAADER BANK AG;N;1500000.00000000;2;N;PST_BF2;;2018-06-25;;2025-06-25;1;2.5;2024-03-25;2024-06-25;1.000000000;1.0000000;6;;100000;N;;100000;FRAV;SingleAuction;BALFR;BAADER BANK AG;QPF;VRR;11:00:00;13:00:00;EUR;;2018-07-23;2025-06-20;GIROSAMMELVERWAHRUNG;SSQ_SUPPORTED;;;;;;;N
"""#
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