import Foundation
let pattern = #"^(.*)(\|.*)(\n(?:(?!msgstr).*\n)*)(msgstr)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
translate italian ch1_669664cb:| mc
# "Hey."
msgstr "Hey."
translate italian ch1_177d3eb2:| mc
# "What are you doing?"
msgstr "What are you doing?"
translate italian ch1_25d98ea6:| m p_1a
# "I'm in charge of the breakfast, remember?"
msgstr "I'm in charge of the breakfast, remember?"
translate italian ch1_7ae83e04:|
# "Oh, that's right."
msgstr "Oh, that's right."
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1$4$3$2"#
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