import Foundation
let pattern = #"([a-zA-Z]+\s+[\d:]+\s[\d:]+)\s(\S+)\s(\S+):\s([\d]+-[\d]+-[\d]+\s[\d]+:[\d]+:[\d]+)?\s?(.*)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
Apr 15 10:34:05 scisp1 couriertcpd: Connection, ip=[::ffff:10.224.0.141]
Apr 15 10:34:05 scisp1 couriertcpd: LOGIN: ip=[::ffff:10.224.0.141], command=LOGIN
Apr 15 10:34:05 scisp1 couriertcpd: LOGIN: ip=[::ffff:10.224.0.141], username=61881227312
Apr 15 10:34:05 scisp1 couriertcpd: LOGIN, user=61881227312, ip=[::ffff:10.224.0.141], port=[59372], protocol=IMAP
Apr 15 10:34:51 scisp1 couriertcpd: TIMEOUT, user=61261886140, ip=[::ffff:10.224.0.141], headers=0, body=0, rcvd=747751, sent=43459, time=1835
Apr 15 10:34:55 scisp1 couriertcpd: TIMEOUT, user=61881226690, ip=[::ffff:10.224.0.141], headers=0, body=0, rcvd=242597, sent=10564, time=1812
Apr 15 10:34:55 scisp1 couriertcpd: LOGOUT, user=61881227312, ip=[::ffff:10.224.0.141], headers=0, body=438023, rcvd=1300, sent=447936, time=50
Apr 15 10:35:04 scisp1 exim[24212]: 2016-04-15 10:35:04 no IP address found for host 103-55-116-16.sipcity.com.au (during SMTP connection from [103.55.116.16])
Apr 15 10:35:04 scisp1 exim[24212]: 2016-04-15 10:35:04 1aqriy-0006IW-8u <= support@sipsolutions.com.au H=(scvm1.sipcity.com.au) [103.55.116.16] P=esmtps X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 S=34149 id=Asterisk-4-132550271-61385471055-990@scvm1
Apr 15 10:35:06 scisp1 exim[24213]: 2016-04-15 10:35:06 1aqriy-0006IW-8u => bp@grindalpatrick.com.au R=dnslookup T=remote_smtp H=filter1.grinda-1.mailguard.com.au [103.248.191.202] X=TLS1.0:RSA_AES_128_CBC_SHA1:128 C="250 2.0.0 Ok: queued as C2CB94261A3 UID 571037396f7ee5bc"
Apr 15 10:35:06 scisp1 exim[24213]: 2016-04-15 10:35:06 1aqriy-0006IW-8u Completed
Apr 15 10:35:08 scisp1 exim[24231]: 2016-04-15 10:35:08 no IP address found for host 103-55-116-18.sipcity.com.au (during SMTP connection from [103.55.116.18])
Apr 15 10:35:08 scisp1 exim[24231]: 2016-04-15 10:35:08 1aqrj2-0006Ip-7E <= customerservice@sipcity.com.au H=(scfax1.sipcity.com.au) [103.55.116.18] P=esmtps X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 S=250689 id=20160415003508.GA29682@scfax1.sipcity.com.au
Apr 15 10:35:10 scisp1 exim[24232]: 2016-04-15 10:35:10 1aqrj2-0006Ip-7E => admin@northernurology.com.au R=dnslookup T=remote_smtp H=remote.northernurology.com.au [58.108.247.139] X=TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256 C="250 2.6.0 <20160415003508.GA29682@scfax1.sipcity.com.au> [InternalId=9647] Queued mail for delivery"
Apr 15 10:35:10 scisp1 exim[24232]: 2016-04-15 10:35:10 1aqrj2-0006Ip-7E -> darcy@northernurology.com.au R=dnslookup T=remote_smtp H=remote.northernurology.com.au [58.108.247.139] X=TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256 C="250 2.6.0 <20160415003508.GA29682@scfax1.sipcity.com.au> [InternalId=9647] Queued mail for delivery"
Apr 15 10:35:13 scisp1 exim[24232]: 2016-04-15 10:35:13 1aqrj2-0006Ip-7E => suki01@gmail.com R=dnslookup T=remote_smtp H=gmail-smtp-in.l.google.com [74.125.68.26] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 C="250 2.0.0 OK 1460680513 fl6si10358337pad.46 - gsmtp"
Apr 15 10:35:14 scisp1 exim[24232]: 2016-04-15 10:35:14 1aqrj2-0006Ip-7E => admin@coralseaent.com.au R=dnslookup T=remote_smtp H=aspmx.l.google.com [74.125.68.27] X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 C="250 2.0.0 OK 1460680514 vz3si1300160pab.93 - gsmtp"
Apr 15 10:35:14 scisp1 exim[24232]: 2016-04-15 10:35:14 1aqrj2-0006Ip-7E Completed
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
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