import Foundation
let pattern = #"<div[^>]*data-oembed-url=["'](.*?)["'].*?>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"<p><strong>Integer</strong> ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a <em>libero</em>. In <u>auctor</u> lobortis lacus. Curabitur suscipit suscipit tellus. Fusce convallis metus id felis luctus adipiscing.</p> <div data-oembed-url="https://www.youtube.com/watch?v=WwHy2hotU6c" data-> <div style="left: 0px; width: 100%; height: 0px; position: relative; padding-bottom: 56.2493%;"><iframe allowfullscreen="true" frameborder="0" mozallowfullscreen="true" src="https://www.youtube.com/embed/WwHy2hotU6c?wmode=transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1" style="top: 0px; left: 0px; width: 100%; height: 100%; position: absolute;" webkitallowfullscreen="true"></iframe></div> </div> <ol> <li>Um</li> <li>Dois</li> <li>Três</li> </ol> <div data-oembed-url="http://pt.slideshare.net/josephj/from-hacker-to-programmer-w-webpack-babel-and-react"> <div style="left: 0px; width: 100%; height: 0px; position: relative; padding-bottom: 83.5282%;"><iframe allowfullscreen="true" frameborder="0" mozallowfullscreen="true" src="https://www.slideshare.net/slideshow/embed_code/key/4guX8biZafHvI7" style="top: 0px; left: 0px; width: 100%; height: 100%; position: absolute;" webkitallowfullscreen="true"></iframe></div> </div> <ul> <li>Primeiro</li> <li>Segundo</li> <li>Terceiro</li> </ul> <p>In consectetuer turpis ut velit. Duis lobortis massa imperdiet quam. Curabitur at lacus ac velit ornare lobortis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Suspendisse nisl elit, rhoncus eget, elementum ac, condimentum eget, diam.</p> <p>Suspendisse faucibus, nunc et pellentesque egestas, lacus ante convallis tellus, vitae iaculis lacus elit id tortor. Quisque rutrum. Curabitur blandit mollis lacus. Duis leo. Suspendisse potenti.</p>"#
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