import Foundation
let pattern = #"<div.*?price.*?>(\d+\.\d+)<\/div>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
<section data-v-7630ac7c="" view-more-component="">
<div data-v-5c5571e4="" table-component="" data-v-7630ac7c="">
<header data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="">Price</div>
<div data-v-5c5571e4="" quantity="" right-align="">Quantity</div>
<div data-v-5c5571e4="" date="">Time</div></header> <section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8611.00</div>
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div>
<div data-v-5c5571e4="" date="">13:27:17</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8612.30</div>
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div>
<div data-v-5c5571e4="" date="">13:03:12</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8517.70</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.04983464</div>
<div data-v-5c5571e4="" date="">12:51:24</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8608.70</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.59195657</div>
<div data-v-5c5571e4="" date="">12:32:26</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8521.70</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.02990000</div>
<div data-v-5c5571e4="" date="">11:29:57</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8545.20</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.26101049</div>
<div data-v-5c5571e4="" date="">01:20:35</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8462.30</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.00674370</div>
<div data-v-5c5571e4="" date="">23:49:47</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8539.30</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.05799985</div>
<div data-v-5c5571e4="" date="">23:00:53</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8446.00</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.02345439</div>
<div data-v-5c5571e4="" date="">22:30:43</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8540.00</div>
<div data-v-5c5571e4="" quantity="" right-align="">0.25500001</div>
<div data-v-5c5571e4="" date="">21:55:28</div></section></div>
<section data-v-7630ac7c="" id="my-trades"><div data-v-75ce2ffa="" data-v-7630ac7c="" button-component="" class="viewmore fullbutton">
<button data-v-75ce2ffa=""></button>
<!----> <span data-v-75ce2ffa="" text="">View More</span> <!----></div> <!----></section> <!----> <!----></section> <!---->
"""#
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