import Foundation
let pattern = ##"^#+\s*(.*?)\{#(.*)\}"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
# Scoring import {#calculate-score}
Loads Scoring*csv files from the WebDAV and import them locally.
While processing and when finished, the file gets renamed on the WebDAV.
# Response export {#unsubscribe-export}
Exports unsubscribes, bounces and complaints and stores the export files
on an SFTP of the customer.
# Campaign uploader {#15mins-campaign-uploader}
Fetches a ZIP from the customers SFTP.
This ZIP must contain a JSON and a HTML. Images must be stored in a `img`
folder. Size limit for an image is 1 MB. Images will be uploaded to the
Suite media library. Campaign meta data will be read from the JSON.
**This process is currently deactivated**
# ecard invite a friend landing page {#recommendation-form}
A form to invite friends. The friends data will be stored in the Suite account.
# Pretrip form {#pretrip-information-form}
Asking the end user to provide more more data before they arrive.
# Signup pages and preference center {#preference-center}
Pages to subscribe and to manage personal data.
"""##
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