import Foundation
let pattern = #"(?<AttributeName>(?:src|background))\s*=\s*["'](?<AttributeValue>(?:[^"'])*)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
<html>
<table id='tblTest' border="1">
<tr>
<td colspan="1" >CellA</td>
</tr>
<tr>
<td>Click<a href="http://mail.google.com" >here</a></td>
</tr>
<tr>
<td>
<img src="http://wcrustine.org/Portals/0/click_here_button.jpg" style="width:100px;height:100px;"></img></td>
</tr>
<tr>
<td background="https://ci6.googleuser.com/proxy/d-NpaBfbly5hKSic8pomfvI4scwJqkF2wwmRqEB4stR8jfO7jo-Sx-JcVLfIXqw5gJnd05Rl0aY3evT2DhmS6he85ifLGQA=s0-d-e1-ft#http://newsletter.mydomain.com/template/header_bg.jpg" bgcolor="#336699" height="100" style="background:url(https://ci5.googleuser.com/proxy/d-NpaBfbly5hKSic8pomfvI4scwJqkF2wwmRqEB4stR8jJrvfO7jo-Sx-JcVLfIgJnd05Rl0aY3evT2DhmS6LU85ifLGQA=s0-d-e1-ft#http://newsletter.mydomain.com/template/header_bg.jpg) top left repeat #336699" width="">
</td>
</tr>
</table>
</html>
"""##
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