Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active March 1, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/cc9d7eacd2cab75047b5f2f1a4ffa4c8 to your computer and use it in GitHub Desktop.
Save laevandus/cc9d7eacd2cab75047b5f2f1a4ffa4c8 to your computer and use it in GitHub Desktop.
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