import Foundation
let pattern = #"(?<=<!-- Template Nav Start -->[\n])([\n\s\S]*?)(?=[\s\n]*?<!-- Template Nav End -->)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:site_name" content="WD Proposal">
<meta property="og:title" content="Your new outdoor living space.">
<meta property="og:image" content="https://www.WhitmerDecks.com/Proposal/eProposal/img/wdLogoName.png">
<meta property="og:description" content="Here you can view the proposal from Whitmer Decks, LLC">
</meta>
<!-- General scripts & styles -->
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/cwBootstrap.css">
<script src="js/cwBootstrap.js" defer></script>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet">
<!-- Page title and end of general head -->
<title>WD Proposal</title>
<!-- Page specific scripts and styles -->
<script src="js/about.js" defer></script>
</head>
<body>
<!-- Template Nav Start -->
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li>Item X</li>
<li>Item Y</li>
</ul>
</nav>
<!-- Template Nav End -->
<p>
The about page
</p>
</body>
</html>
"""#
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