import Foundation
let pattern = #"((\b\p{Upper}\w+)|(\b\p{Upper}\w{1,5}\.)) ?( ((\b\p{Lower}{1,6})|(\b\p{Upper}\w+)|((\b\p{Upper}\w{1,5}\.)))){0,4} v\.? ((\b\p{Upper}\w+)|(\b\p{Upper}\w{1,5}\.)) ?( ((\b\p{Lower}{1,6})|(\b\p{Upper}\w+)|((\b\p{Upper}\w{1,5}\.)))){0,4}"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
This is not a case. But this is: Riley v. California. And riley v. california also isn't.
Riley v. California and Mapp v. Ohio "United Zinc & Chemical Co. v. Britt" should all count, but not MAPP V. OHIO!
Both R.A. Peacock v. Lubbock Compress Company and Battalla v. State of New York have complex names.
But also, Craggan v. IKEA USA and this Case: Edwards v. Honeywell, Inc..
"""#
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