import Foundation
let pattern = #"\-\s\*data\sintegrity\:\*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
- *Financial:* <yes or no. If yes, explain why>
- *Legal/Regulatory:* <yes or no. If yes, explain why>
- *Data Integrity:* <yes or no. If yes, explain why>
- *Patient Safety:* <yes or no. If yes, explain why>
- *CyberSecurity/Information Security:* <yes or no. If yes, explain why>
- *Financial:* <yes/no>
- *Legal/Regulatory:* <yes/no>
- *Data Integrity:* <yes/no>
- *Patient Safety:* <yes/no>
- *CyberSecurity/Information Security:* <yes/no>
- *Financial:* <yes or no. If yes, explain why>No
- *Legal/Regulatory:* <yes or no. If yes, explain why> Yes
- *Data Integrity:* No
- *Patient Safety:* Yes, this was an issue because of XYZ.
- *CyberSecurity/Information Security:* <yes or no. If yes, explain why>
- *Financial:* no
- *Legal/Regulatory:* no
- *Data Integrity:* no
- *Patient Safety:* no
- *CyberSecurity/Information Security:* no
- *Financial:* N
- *Legal/Regulatory:* N
- *Data Integrity:* N
- *Patient Safety:* N
- *CyberSecurity/Information Security:* N
- *Financial:* <yes or no. If yes, explain why>N
- *Legal/Regulatory:* <yes or no. If yes, explain why> N
- *Data Integrity:* <yes or no. If yes, explain why>N
- *Patient Safety:* <yes or no. If yes, explain why>N
- *CyberSecurity/Information Security:* <yes or no. If yes, explain why> 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