import Foundation
// WARNING: You included a flag that Swift doesn't support: U
// When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
// As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
let pattern = #"class="rasschetSredstv".*<td>.*?href="(?P<URL>.*)".*(?P<DATE>15.03.\d\d\d\d).*</td>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = #"""
<div class="rasschetSredstv">
<table class="tab td_tac" border="0" cellpadding="10" cellspacing="0">
<tbody><tr>
<th width="17%">Январь</th>
<th width="17%">Февраль</th>
<th width="17%">Март</th>
<th width="17%">Апрель</th>
<th width="17%">Май</th>
<th width="17%">Июнь</th>
</tr>
<tr>
<td>
<a target="_blanc" href="/upload/iblock/2da/otchet_ss_310117.pdf">Скачать</a><br>
pdf(201 кБ)<br> (14.02.2017 12:44:00) </td>
<td>
<a target="_blanc" href="/upload/iblock/aa8/otchet_rss_na_sayt.pdf">Скачать</a><br>
pdf(196 кБ)<br> (15.03.2017 18:49:00) </td>
<td>
<a target="_blanc" href="/upload/iblock/71e/raschet_ss_na_sayt.pdf">Скачать</a><br>
pdf(197 кБ)<br> (14.04.2017 18:23:00) </td>
<td>
----
</td>
<td>
----
</td>
<td>
----
</td>
</tr>
<tr>
<th>Июль</th>
<th>Август</th>
<th>Сентябрь</th>
<th>Октябрь</th>
<th>Ноябрь</th>
<th>Декабрь</th>
</tr>
<tr>
<td>
----
</td>
<td>
----
</td>
<td>
----
</td>
<td>
----
</td>
<td>
----
</td>
<td>
----
</td>
</tr>
</tbody></table>
</div>
"""#
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