import Foundation
let pattern = #"<td data-table-header="The bank [^"]+">([\d|,|.\+]+)<\/td>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
<div class='index-currency-table'>
<!--http://1000hz.github.io/bootstrap-validator/#validator-usage-->
<div class="row">
<div class="col-xs-12">
<table class="table--exchange table--exchange--responsive">
<thead>
<tr>
<th scope="col">Currency</th>
<th scope="col">Nominal</th>
<th scope="col">The bank buys</th>
<th scope="col">The bank sells</th>
<th scope="col">BNB</th>
</tr>
</thead>
<tbody>
<tr>
<td data-table-header="Currency">
<a href="/en/rates-indexes/currency-rates/USD/" target="_self" title="United States Dollar">
<span class="flag-icon flag-icon-us"></span> USD
</a>
</td>
<td data-table-header="Nominal">1</td>
<td data-table-header="The bank buys">1.581200</td>
<td data-table-header="The bank sells">1.646100</td>
<td data-table-header="BNB">1.614390</td>
</tr>
</tbody>
</table>
</div>
<!--col-->
</div>
<!--row-->
</div>
"""##
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