import Foundation
let pattern = #"(?<=by\n)\w.+"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
MIT Sample:
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
----------------------------------------------
Virginia Tech Sample:
FORMATTING VARIABLES AND TYPEFACE VARIATIONS
OF
DOT-MATRIX PRINT AND THEIR EFFECT
ON
READING COMPREHENSION AND READING SPEED
by
James A. Holmes
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
Vocational and Technical Education
March, 1986
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