import Foundation
let pattern = #"""
\/\*[^*]*\*+([^\/*][^*]*\*+)*\/
"""#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* @TODO:
- Notes...
*/
/* = = = Set Base Font Size = = = So 1em = 10px / 1.8em = 18px etc */
body { font-size: 62.5% }
body, ul, li {
color: #333333;
font-family: 'Open Sans', verdana, arial, helvetica, helve, sans-serif;
font-weight: 400;
/* font-size: 1.4em; Default font = 14px normal weight Open Sans using #333333 */
line-height: 1.25em;
}
body {
margin: 0; /* Nav bar and footer need to fit edge to edge so no margins! */
padding: 0;
top: 0;
border-bottom: 1px #1C1C1C solid; /* To remove white space under the popup footer */
}
/* = = = Typography = = = */
/* Font choice was carefully considered, Open Sans is very popular so many visitors will already have it cached. */
/* Monda & Shadows into light are also quite popular so may be cached too */
/* = = = Headings = = = */
h1, h2, h3, h4, h5, h6 {
font-family: 'Monda', 'Trebuchet MS', verdana, arial, helvetica, helve, sans-serif;
font-weight: 400;
line-height: 1.2em;
letter-spacing: 1px;
color: #333333;
margin-top: 10px;
margin-bottom: 20px;
}
"""##
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