import Foundation
let pattern = #"[a-zA-Z0-9\-\_]*\.[a-zA-Z0-9\-\_]*\.\w{1,3}(\.\w{1,3})?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
service.mensfitness.co.uk
updates.mensfitness.co.uk
promo.mensfitness.co.uk
deals.mensfitness.co.uk
subs.mensfitness.co.uk
service.healthandfitnessoneline.co.uk
updates.healthandfitnessoneline.co.uk
promo.healthandfitnessoneline.co.uk
deals.healthandfitnessoneline.co.uk
subs.healthandfitnessoneline.co.uk
service.custompc.co.uk
updates.custompc.co.uk
promo.custompc.co.uk
deals.custompc.co.uk
subs.custompc.co.uk
service.digitalslrphoto.com
updates.digitalslrphoto.com
promo.digitalslrphoto.com
deals.digitalslrphoto.com
subs.digitalslrphoto.com
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"'\g<0>'"#
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