import Foundation
let pattern = #"^(Name=")(Show_Type"|Licensing_Window_End"|Display_As_New")(\s+Value="[A-Za-z0-9-:\s]+")([\/>\s]+)(.*)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
App_Data App="MOD" Name="Genre" Value="Series"/><App_Data App="MOD"
Name="Show_Type" Value="Series"/><App_Data App="MOD" Name="Billing_ID"
Value="10092"/><App_Data App="MOD" Name="Licensing_Window_Start"
Value="2019-05-07 00:00:00"/><App_Data App="MOD"
Name="Licensing_Window_End" Value="2019-05-13 23:59:59"/><App_Data
App="MOD" Name="Preview_Period" Value="0"/><App_Data App="MOD"
Name="Display_As_New" Value="4"/><App_Data App="MOD"
Name="Display_As_Last_Chance" Value="7"/><App_Data App="MOD"
Name="Provider_QA_Contact" Value="NBC Universal"/><App_Data App="MOD"
Name="Suggested_Price" Value="0.00"/><App_Data App="MOD"
"""#
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