import Foundation
let pattern = #"(Certified by\n?|APPROVED:\n?|approved by:\n?)(\w[^,?].+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
SOME ASPECTS OF RADIATION INDUCED NUCLEATION IN WATER
by
Chih-Ping Tso
B.Tech., Loughborough University of Technology, United Kingdom
(1968)
Submitted in Partial Fulfillment of
the Requirements for the Degree of
Master of Science
at the
Massachusetts Institute of Technology
August 1970
Signature of Author
Department of Nuclear Engineering
Certified by
Thesis Supervisor
Accepted by
Chairman, Departmental Committee
Archives on Graduate Students
MASS. INST. TECH.
SEP 21 1970
LIBRARIES
--------------------------------------
ASSESSMENT OF A MENTOR PROGRAM
ON SELF-CONCEPT AND ACHIEVEMENT VARIABLES
OF MIDDLE SCHOOL UNDERACHIEVERS
by
Helene Aiello
Dissertation submitted to the faculty of the Virginia
Polytechnic Institute and State University in partial
fulfillment of the requirements for the degree of
Doctor of Education
in
Administration and Supervision
Of
Special Education
APPROVED:
Philip R. Jones, Chairman Kenneth Underwood
Sylvia Auton
Shirley Jones Ronald McKeen
February 22, 1988
Blacksburg, Virginia
"""#
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