-
-
Save laevandus/cc9d7eacd2cab75047b5f2f1a4ffa4c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let descExpression = try! NSRegularExpression(pattern: "^\\s*desc [\"']{1}([^\"]*)[\"']{1}", options: []) | |
// … | |
let description = (0..<lineIndex) | |
.reversed() | |
.prefixMap({ index -> String? in | |
// Get a line | |
let line = lines[index] | |
// Use regular expression | |
guard let match = descExpression.firstMatch(in: line) else { return nil } | |
// Regular expression contains a capture group, therefore looking for 2 ranges | |
guard match.numberOfRanges >= 2 else { return nil } | |
// When there are matches then extract description | |
guard let range = Range(match.range(at: 1), in: line) else { return nil } | |
return String(line[range]) | |
}) | |
.reversed() | |
.joined(separator: "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment