import Foundation
let pattern = #"^(?=(?!(.)\1)([^\DO:105-93+30])(?-1)(?<!\d(?<=4))).[^\WHY?]$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
┌──────────────────────────────┐
│ What is the Meaning of Life? │
│ Potential Answers │
└──────────────────────────────┘
Dear regex, please pick one of the following lines.
---
The meaning of life, according to the Oxford English Dictionary, is the condition or attribute of living or being alive.
Why should life have a meaning? What a stupid question.
42
Meaning of life has to be qualified. The meaning of your life to you is not the same as the meaning of your life to a dictator. And what about the meaning of a bee's life, or of a bacteria's life. In other words, see the two previous answers, which can be summarized by 'what a stupid question.'
"""#
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