import Foundation
let pattern = #"(TEST_CASE_NAME.*?:(.*?)\n.*?PRIORITY.*?:(?!P3)(\w\d).*?=cut)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = #"""
=head2 Gen_001
TEST_CASE_NAME :GEN_001
PRIORITY :P0
RELEASE_INTRODUCED :7.4 AUTOMATED :YES
STEP_NAME : step1
STEP_DESC :Example desc for understanding STEP_RESULT :Example result for understanding
=cut
=head2 Gen_003
TEST_CASE_NAME :GEN_003
PRIORITY :P1
RELEASE_INTRODUCED :7.4 AUTOMATED :NO
STEP_NAME : step1
STEP_DESC :Example desc for understanding second testcase
STEP_RESULT :Example result for understanding second testcase
=cut
=head2 Gen_004
TEST_CASE_NAME :GEN_004
PRIORITY :P3
RELEASE_INTRODUCED :7.4 AUTOMATED :NO
STEP_NAME : step1
STEP_DESC :Example desc for understanding third testcase
STEP_RESULT :Example result for understanding third testcase
=cut
"""#
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