import Foundation
let pattern = #".*сума[:\s]+(?<suma>\d{1,5}[.,]\d{1,2}).*(?<pan>[0-9X\*]{16}).*термі.*(?<terminalId>[A-Z0-9]{8,10}).*код автор.*(?<approvalCode>\d{1,6}).*чек.*(?<chq>\d{1,10}).*rrn[:\s]+(?<rrn>\d{1,12})"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = ##"ПРИВАТБАНК\nМАГАЗИН Xxxxxx\nвул.Кручковського,буд.5Б\nс.Нагачiв\nФОП XXXXX Б.Б.\n2517109510\nОПЛАТА\nСУМА:1.25 ГРН\n\n************4211\nContactless\nMASTERCARDAID: A0000000041010\nТЕРМIНАЛ # S11800CC20 ЛЮТ 2024 18:49:02\nКОД АВТОРИЗ.: 463521ЧЕК N: 436084\nRRN: 080469527205\n\n(с)SSI 2023 PRIVATBANK v.T5PRVa_.04E\nSmartPos_EMV 01.001\n"##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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