import Foundation
let pattern = #"(.*)\n(.*)\n(.*)\n(.*)\n(.*)\n(.*)\s*"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
1 Question
2 Answer 1
3 Answer 2
4 Answer 3
5 Answer 4
6 Answer 5
7 Question
8 Answer 1
9 Answer 2
10 Answer 3
11 Answer 4
12 Answer 5
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"<h1>$1</h1><p>$2</p><br><p>$3</p><br><p>$4</p><br><p>$5</p>"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
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