import Foundation
let pattern = #"Executed\s+as\s+user:\s+[\w\\]+\.|\[SQLSTATE \d+\] \(\w+ \d+\)\s*\.?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"Executed as user: Company\SA_SQLA. Warning: Null value is eliminated by an aggregate or other SET operation. [SQLSTATE 01003] (Message 8153) Warning: Null value is eliminated by an aggregate or other SET operation. [SQLSTATE 01003] (Message 8153) Warning: Null value is eliminated by an aggregate or other SET operation. [SQLSTATE 01003] (Message 8153) Warning: Null value is eliminated by an aggregate or other SET operation. [SQLSTATE 01003] (Message 8153) The OLE DB provider "MSOLEDBSQL" for linked server "linked_server" does not contain the table ""databasename"."dbo"."table"". The table either does not exist or the current user does not have permissions on that table. [SQLSTATE 42000] (Error 7314) 2023-04-04 08:19:05.797: Fetching data, please wait... [SQLSTATE 01000] (Error 50000) 2023-04-04 08:19:05.797: Fetching data for Mar 4 2023 8:19AM, please wait... [SQLSTATE 01000] (Error 50000) 2023-04-04 08:20:07.813: Fetching data done! Starting Calculation, please wait... [SQLSTATE 01000] (Error 50000) 2023-04-04 08:20:22.657: Calculation Done! forwarding data, please wait... [SQLSTATE 01000] (Error 50000). The step failed."#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$0\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