import Foundation
let pattern = #"<(.)*>"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
<p><b>Vancouver</b> (<span class="rt-commentedText nowrap"><span class="IPA nopopups noexcerpt"><a href="/wiki/Help:IPA/English" title="Help:IPA/English">/<span style="border-bottom:1px dotted"><span title="'v' in 'vie'">v</span><span title="/æ/: 'a' in 'bad'">æ</span><span title="'n' in 'nigh'">n</span><span title="/ˈ/: primary stress follows">ˈ</span><span title="'k' in 'kind'">k</span><span title="/uː/: 'oo' in 'goose'">uː</span><span title="'v' in 'vie'">v</span><span title="/ər/: 'er' in 'letter'">ər</span></span>/</a></span> <span class="nowrap" style="font-size:85%">(<span class="unicode haudio"><span class="fn"><span style="white-space:nowrap;margin-right:.25em;"><a href="/wiki/File:EN-Vancouver.ogg" title="About this sound"><img alt="About this sound" src="//upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/11px-Loudspeaker.svg.png" decoding="async" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/17px-Loudspeaker.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/22px-Loudspeaker.svg.png 2x" data-file-width="20" data-file-height="20" width="11" height="11"></a></span><a href="//upload.wikimedia.org/wikipedia/commons/4/44/EN-Vancouver.ogg" class="internal" title="EN-Vancouver.ogg">listen</a></span></span>)</span></span> <a href="/wiki/Help:Pronunciation_respelling_key" title="Help:Pronunciation respelling key"><i title="English pronunciation respelling">van-<span style="font-size:90%">KOO</span>-vər</i></a>) is a major city in <a href="/wiki/Western_Canada" title="Western Canada">western Canada</a>, located in the <a href="/wiki/Lower_Mainland" title="2016 Canadian Census">Lower Mainland</a> region of <a href="/wiki/British_Columbia" title="British Columbia">British Columbia</a>. As the <a href="/wiki/List_of_cities_in_British_Columbia" title="List of cities in British Columbia">most populous city</a> in the province, the <a href="/wiki/2016_Canadian_Census" class="mw-redirect" title="">2016 census</a> recorded 631,486 people in the city, up from 603,502 in 2011. The <a href="/wiki/Greater_Vancouver" title="Greater Vancouver">Greater Vancouver</a> area had a population of 2,463,431 in 2016, making it the <a href="/wiki/List_of_census_metropolitan_areas_and_agglomerations_in_Canada#List" title="">third-largest metropolitan area in Canada</a>. Vancouver has the highest population density in Canada, with over 5,400 people per square kilometre.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5">[5]</a></sup><sup id="cite_ref-6" class="reference"><a href="#cite_note-6">[6]</a></sup> Vancouver is one of the most <a href="/wiki/Ethnic_origins_of_people_in_Canada" title="Ethnic origins of people in Canada">ethnically</a> and linguistically diverse cities in Canada: 52 percent of its residents are not native English speakers,<sup id="cite_ref-pop2006-Van_7-0" class="reference"><a href="#cite_note-pop2006-Van-7">[7]</a></sup><sup id="cite_ref-8" class="reference"><a href="#cite_note-8">[8]</a></sup> 48.9 percent are native speakers of neither English nor French, and 50.6 percent of residents belong to <a href="/wiki/Visible_minority" title="Visible minority">visible minority</a> groups.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9">[9]</a></sup>
</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