import Foundation
let pattern = #"((([a-zA-Z]+)(\s)*(:))){1}(\s)*(((?:[\[])?(?:["])?((.)*)(?:["])?(?:[\]]?))?)?([,]?)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
{
id: "00000000-0000-0000-0000-000000000000",
numericId: 0,
accountId: "00000000-0000-0000-0000-000000000000",
accountCode: "",
groups: ["group1"],
isTest: true,
ftpInfo: {
host: "127.0.0.1",
port: 21,
homeDirectory: "/",
fileMask: "*.txt",
username: "",
password: "",
isPassive: true,
isBinary: true
},
contactName: "",
contactEmailAddress: "",
createdOn: "1900-01-01T00:00:00",
modifiedOn: "1900-01-01T00:00:00"
}
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #""$3": $7$11"#
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