import Foundation
let pattern = #"(<(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\s+(?=((?:"[\S\s]*?"|'[\S\s]*?'|(?:(?!\/>)[^>])?)+))\3)?\s*>)[\S\s]*?<\/\2\s*(?=>))|(?:\/?[\w:]+\s*\/?)|(?:[\w:]+\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]?)+\s*\/?)|\?[\S\s]*?\?|(?:!(?:(?:DOCTYPE[\S\s]*?)|(?:\[CDATA\[[\S\s]*?\]\])|(?:--[\S\s]*?--)|(?:ATTLIST[\S\s]*?)|(?:ENTITY[\S\s]*?)|(?:ELEMENT[\S\s]*?))))>)|([^<>]*?)(querry)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
<body class="mediawiki querry ltr sitedir-ltr ns-0 ns-subject page-International_English_Language_Testing_System skin-vector action-view vector-animateLayout">
<div id="mw-page-base" class="noprint"></div>
<div id="mw-head-base" class="noprint"></div>
<div id="content" class="mw-body" role="main">
<a id="top"></a>
and also a normal querry
<div id="siteNotice"><!-- CentralNotice --></div>
<h1 id="firstHeading" class="firstHeading" lang="en"><span dir="auto">International English Language Testing System</span></h1>
<div id="bodyContent" class="mw-body-content">
<div id="siteSub">From Wikipedia, the free encyclopedia</div>
<div id="contentSub"> (Redirected from <a href="/w/index.php?title=IELTS&redirect=no" title="IELTS">IELTS</a>)</div>
<div id="jump-to-nav" class="mw-jump">
Jump to: <a href="#mw-navigation">navigation</a>, <a href="#p-search">search</a>
</div>
<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><table class="infobox vevent" cellspacing="3" style="border-spacing:3px;width:22em;">
<caption class="summary">International English Language Testing System</caption>
<tr>
<td colspan="2" style="text-align:center;"><a href="/wiki/File:IELTS_logo.svg" class="image"><img alt="IELTS logo.svg" src="//upload.wikimedia.org/wikipedia/commons/thumb/7/7f/IELTS_logo.svg/300px-IELTS_logo.svg.png" width="300" height="113" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/7/7f/IELTS_logo.svg/450px-IELTS_logo.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/7/7f/IELTS_logo.svg/600px-IELTS_logo.svg.png 2x" data-file-width="778" data-file-height="293" /></a></td>
</tr>
<tr>
<th scope="row" style="text-align:left;">Acronym</th>
<td>IELTS</td>
</tr>
<tr>
<th scope="row" style="text-align:left;">Type</th>
<td>Standardized test. Available in 2 versions: "Academic" and "General training".</td>
</tr>
<tr>
<th scope="row" style="text-align:left;">Developer / administrator</th>
<td><a href="/wiki/Cambridge_English_Language_Assessment" title="Cambridge English Language Assessment">Cambridge English Language Assessment</a>, <a href="/wiki/British_Council" title="British Council">British Council</a>, <a href="/wiki/IDP_Education" title="IDP Education">IDP Education</a>.</td>
</tr>
<tr>
<th scope="row" style="text-align:left;">Knowledge/skill(s) tested</th>
<td>Listening, reading, writing and speaking of the <a href="/wiki/English_language" title="English language">English language</a>.</td>
</tr>
<tr>
<th scope="row" style="text-align:left;">Purpose</th>
<td>To assess the English language proficiency of non-native English speakers.</td>
</tr>
"""##
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