import Foundation
let pattern = #"((?<syslog_pri>(^\<\d*\>))|(^(?<facility>[^\.]*)\.(?<severity>[^\s]*)))?\s*(?:%{SYSLOGTIMESTAMP:timestamp}|%{TIMESTAMP_ISO8601:timestamp}) %{SYSLOGHOST:iporhost}(?<messgae>.*)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
<165>
User.Notice 2016-01-02 00:00:33 1.1.66.15 1.1.66.15 02/01/2016 00:00:00 NTP: Synchronization OK.
Local7.Warning 2016-01-02 00:00:43 1.1.5.6 319129: 14w1d: %CDP-4-NATIVE_VLAN_MISMATCH: Native VLAN mismatch discovered on GigabitEthernet1/0/21 (780), with name_a FastEthernet3 (1).
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #""#
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