import Foundation
let pattern = #"^((?:.+?, .+?;)*?(?:[^;\s]+?, .+?)\.)([^;]+?\.).*$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
DI MAIO, P. The Missing Pragmatic Link in the Semantic Web. Business Intelligence Advisory Service Executive Update. v. 8, n. 7, 2008.
ECO, U. Lector in Fabula: la cooperación interpretativa en el texto narrativo. Barcelona: Lumen, 1987
ECO, U. O conceito de texto. São Paulo: T. A. Q. /EDUSP, 1984.
ECO, U. Obra aberta: forma e indeterminação nas poéticas contemporâneas. São Paulo: Perspectiva, 1988.
ECO, U. Os limites da interpretação. São Paulo: Pioneira, 2000.
EDMONDS, B. The Pragmatic Roots of Context. In: PROC. OF THE 2ND INTERNATIONAL AND INTERDISCIPLINARY CONFERENCE ON MODELING AND USING CONTEXT. Berlin; Heidelberg; New York, v. 1688, 1999. Anais… v. 1688, p. 119-132, 1999.
BERNERS-LEE, T. Semantic Web Concepts. 2005a. Disponível em: http://www.w3.org/2005/Talks/0517-boit-tbl. Acesso em: 25 set. 2014
BERNERS-LEE, T. Web for real people. 2005b. Disponível em . Acesso em: 25 set. 2014.
BERNERS-LEE, T.; CAILLIAU, R. WorldWideWeb: Proposal for a HyperText Project. 1990. Disponível em: < http://www.w3.org/Proposal.html >. Acesso em: 13 out. 2014.
BERNERS-LEE, T.; HENDLER, J.; LASSILA, O. The semantic web: a new form of web content that is meaningful to computers will unleash a revolution of new possibilities. New York: Scientific American, 2001. Disponível em: http://www.sciam.com/2001/050lissue/0501berners-lee.html. Acesso em: 13 out. 2014.
"""#
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