import Foundation
let pattern = #" <p[^>]*>((?:(?!<\/p>).)+)<\/p>\s*<blockquote[^>]*>(.*?)<\/blockquote>"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"<p> demo <p>..</p>demo:</p> <p ><img src="http://demo.com/123.jpg" width="100%"/> <br/> <em>Credit: demo2 demo2 </em></p> <p >here1 here1:</p> <blockquote cite="here1"> <p><em>demo3. demo3 demo3 demo3:</em></p> </blockquote> <p >demo4 demo4:</p> <p ><img src="http://demo.com/1234.jpg" width="100%"/> <br/> <em>demo5 demo 5 demo5</em></p> <p >demo6 demo6:</p> <blockquote cite="demo6"> <p><em>demo7 demo7<br/>"#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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