import Foundation
let pattern = #"[A-Z][a-z]+[,;\s\&]*(?:[A-Z][a-z]|\s*et al\.\s*)*(?:, \d{4};?)+"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
(Kim et al., 2004)
(Kim et al., 2004; Sun et al., 2014; Hinuma et al., 2012; Masubuchi, 2013)
( No. 221) with the O/N anions completely disordered in the anion octahedra (Kim et al., 2004; Pors et al., 1988; Fig. 1). The situation of SrTaO2N is more complex, and several structural models have been reported: I4/mcm (No. 140; Gu nther et al., 2000; Zhang et al., 2011; Kim et al., 2004; Clarke et al., 2002), I112/m (No. 12; Yang et al., 2011) and Fmmm (No. 69; Clark et al., 2013).
(Higashi et al., 2008, 2013, 2015; Oehler & Ebbinghaus, 2016; Ueda et al., 2015)
Oehler & Ebbinghaus, 2016;
A further Ta- O/N octahedra displacement [R 5 - (a,0,0), rotation about the c axis] distortion was observed in SrTaO2N. This distortion mode is accompanied by an increased unit-cell distortion that decreases as the temperature increases. Ultimately a second-order phase transition caused by the loss of the R 5 - (a,0,0) mode was observed at 400- 450 K.
Mixed anion materials, in particular oxynitrides, are attracting considerable attention for their novel properties. SrTaO2N and BaTaO2N are two typical perovskite oxynitrides that have been widely studied due to their promising properties that include semiconductivity (Kim et al., 2004), dielectric relaxation (Kim et al., 2004; Sun et al., 2014; Hinuma et al., 2012; Masubuchi, 2013) and photocatalytic activity (Higashi et al., 2008, 2013, 2015; Oehler & Ebbinghaus, 2016; Ueda et al., 2015). Evidently the distortion of the octahedra in perovskite oxynitrides is a key parameter in understanding their interesting properties. Thus, a careful and systematic study of the Ta- O/N octahedra distortion in SrTaO2N and BaTaO2N is likely to enhance our understanding of these fascinating materials.
"""#
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