import Foundation
let pattern = #"^(Em\s\w+,\s)|(Em\s)?\d{1,2}\sde\s\w+\sde\s\d{4}\s(às\s)?\d{1,2}:\d{1,2}([^@]+@\w+\.\w+\.\w+|,\s\sescreveu\:)?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
teste
Em qua, 11 de abr de 2018 às 09:08, wmsouza <teste@hotmail.com> escreveu:
Em 11 de abr de 2018
Não sei
Assunto:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
5 de Abril de 2018 10:50, atendimento@teste.com.br (mailto:atendimento@teste.com.br) escreveu:teste
Em 5 de Abril de 2018 10:30, atendimento@teste.com.br (mailto:atendimento@teste.com.br) escreveu:resposta atendimento
-----------------------------------------------------------
Olá, como vai?
10 de abril de 2018 09:49
10 de abril de 2018 09:49, escreveu:
Em 10 de abril de 2018 09:49
Em 10 de abril de 2018 09:49, escreveu:
> Resposta atendimento
>
>
> 10 de Abril de 2018 09:48, "erick zanetti" > escreveu:
>
>
> Olá, erick,
> A data de entrega do seu trabalho Lista Vetores
> é
> amanhã. Você deseja entregá-lo?
>
>
> Lista Vetores
> Prazo: 7 de abr
> ABRIR
>
> Se você não quiser receber e-mails do Sala de aula, cancele sua inscrição
> .
>
>
>
"""#
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