import Foundation
let pattern = #"^(?i)(?!-+\s+Page\s+\d+-+|Python\s+Regular\s+Expression\s+\d{2}.+\d{4}|.+Python\s+Proprietary|PYTHON\s+RE SPECIFICATION\s+Version.+\s+page\s+\d+|Python\s+Regular\s+Expression\s+Specification).+$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
This module provides regular expression matching operations.
Regular expressions use the backslash character ('\') to indicate special forms
or to allow special characters to be used without invoking their special
meaning.
Python Regular Expression 02 December 1999
Python Proprietary
----------------------- Page 292-----------------------
PYTHON RE SPECIFICATION Version 2.7 [Vol 9, Part Q] page 983
Python Regular Expression Specification
It is important to note that most regular expression operations are available as
module-level functions and RegexObject methods. The functions are shortcuts that
don’t require you to compile a regex object first, but miss some fine-tuning
parameters.
"""#
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