import Foundation
let pattern = #"<li.*?(?!<br).*?\/li>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"Linda Casa Térrea com edícula em São Paulo - Vila Alpina<br /><br /> Casa principal é térrea. Sala de estar, sala de jantar cozinha ampla, 03 dormitórios sendo 01 suíte com closet, 05 vagas cobertas. <br /> Casa principal é térrea. Sala de estar, sala de jantar e cozinha ampla. <li>Teste 1</li><li>Teste 2</li><li>Teste 3</li><br /> Casa dos fundos é um loft moderno com pé direito duplo, mezanino, sala 02 ambientes com lareira, cozinha americana planejada, 01 suíte com armários planejados e hidromassagem, jardim de inverno com espelho d'água. <br /><br /> Jardim com 80 m², espaço gourmet com 02 quiosques. <br /><br /><b> Área construída 300 m² / Área total 380 m²:</b><li>Teste 1</li><li>Teste 2</li><li>Teste 3</li><br /><br /> Valor R$ 800.000,00<br /> <span class="censor">Ref. 3128</span><br /><br /> <br /> <a class="censor link txtLnk ex" href="link=Z2FicmllbGltb3ZlaXMuY29tLmJy" title="Clique para abrir este link" rel="nofollow">teste.com.br</a><br /> <a class="censor link txtLnk nb" href="mailto:teste@teste.com.br" title="Clique para enviar um email a este destinatário" rel="nofollow">teste@teste.com.br</a>"#
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