import Foundation
let pattern = #"(\n\n)?(\n)?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
Intro & Verse
A
Chorus
D.
E.
A...
A...
E...
D.
E.
A...
E...
Interlude
A
A(2)
Asus4
A
Verse 2
A...
[8x]
F#m.
E.
A...
A...
A...
F#m.
E.
A...
D.
E.
A...
Chorus 2
D.
E.
F#m...
A...
E...
D.
E.
F#m...
E...
Turnaround
A...
Asus2...
Asus...
A...
A/D...
Bm7...
Asus...
A...
Bridge
A...
[8x]
F#m
D
A
A
[3x]
F#m
D
E
E
Outro
A...
[4x]
D...
[2x]
A...
[2x]
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"\1"#
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