import Foundation
let pattern = #"^((?:.+\n .+\n)+)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Mr. Lewes reaches this conclusion: "If, therefore, we reflect what a poem _Faust_ is, and that it contains almost every variety of style and metre, it will be tolerably evident that no one unacquainted with the original can form an adequate idea of it from translation," which is certainly correct of any translation wherein something of the rhythmical variety and beauty of the original is not retained. That very much of the rhythmical character may be retained in English, was long ago shown by Mr. Carlyle, in the passages which he translated, both literally and rhythmically, from the _Helena_ (Part Second). In fact, we have so many instances of the possibility of reciprocally transferring the finest qualities of English and German poetry, that there is no sufficient excuse for an unmetrical translation of _Faust_. I refer especially to such subtile and melodious lyrics as "The Castle by the Sea," of Uhland, and the "Silent Land" of Salis, translated by Mr. Longfellow; Goethe's "Minstrel" and "Coptic Song," by Dr. Hedge; Heine's "Two Grenadiers," by Dr. Furness and many of Heine's songs by Mr Leland; and also to the German translations of English lyrics, by Freiligrath and Strodtmann.
> Life of Goethe (Book VI.).
> Mr. Lewes gives the following advice: "The English reader would perhaps best succeed who should first read Dr. Anster's brilliant paraphrase, and then carefully go through Hayward's prose translation." This is singularly at variance with the view he has just expressed. Dr. Anster's version is an almost incredible dilution of the original, written in _other_ metres; while Hayward's entirely omits the element of poetry.
> Foreign Review, 1828.
> When Freiligrath can thus give us Walter Scott:—
"Kommt, wie der Wind kommt,
Wenn Wälder erzittern
Kommt, wie die Brandung
Wenn Flotten zersplittern!
Schnell heran, schnell herab,
Schneller kommt Al'e!—
Häuptling und Bub' und Knapp,
Herr und Vasalle!"
And now, for something completely different.
I offer to you a limmerick, composed with care and not nearly enough caffeine to write properly.
There once was a lady in Rome,
Whose face was like that of a gnome.
She asked for the hand
Of a man with much land
And sat on his lawn, now her home.
...Yeah.
Oh! and here's this, too.
abra ca dabra op
lorem ipsum holy this
line doesn't have
an indent but it's in
continuity of the
structure that sits together.
This one ignores
the last line since
it doesn't have a child with spaces
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"```\n\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