import Foundation
let pattern = #"(?:Nameservers[^\n]*\n.*?)*(?>Hostname:\s+([\S]+)\n)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = #"""
Domain: myDomain.int
DNS: myDomain.int
Registered: 2013-05-17
Expires: 2023-05-31
Registration period: 1 year
VID: no
DNSSEC: Unsigned delegation, DNSSEC disabled, no records
Status: Active
Registrant
Handle: ***N/A***
Name: myName Corp
Attention: Reggie Person
Address: SomeStreet 17
Postalcode: numericZip
City: myCity
Country: XX
Phone: +12 34 56 78 90
Nameservers
Hostname: ns1.dom.ext
Hostname: ns2.dom.ext
Hostname: ns3.dom.ext
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = ####"### push(@nameservers, $1); <-- it works :)\n"####
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