import Foundation
let pattern = #"^(?<time>[^ ][-:+0-9TZ]+|[[:upper:]][[:lower:]]{2}[ -:+0-9TZ]*)\s+(?<messages>.*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .allowCommentsAndWhitespace])
let testString = ##"""
# suse
2017-09-19T10:13:33+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: AMQP server on 10.20.97.42:5672 is unreachable: [Errno 113] EHOSTUNREACH. Trying again in 2 seconds.
2017-09-19T10:13:47+00:00 d2c-76-8a-ac-64-6e ceilometer-expirer: Unable to reconnect to the primary mongodb after 10 retries. Giving up.
2017-09-19T10:13:47+00:00 d2c-76-8a-ac-64-6e ceilometer-expirer: ServerSelectionTimeoutError: 10.60.2.83:27017: [Errno 111] ECONNREFUSED,10.60.2.85:27017: [Errno 111] ECONNREFUSED,10.60.2.84:27017: [Errno 111] ECONNREFUSED
2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Polling pollster cpu_util in the context of meter_source
2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Polling pollster memory.usage in the context of meter_source
2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Cannot inspect data of MemoryUsagePollster for 7f042103-7954-450e-8d8a-c7e48f8c29f4, non-fatal reason: Failed to inspect memory usage of instance <name=instance-0003cb17, id=7f042103-7954-450e-8d8a-c7e48f8c29f4>, can not get info from libvirt.
2017-07-28T12:26:02+00:00 de4-1d-2d-0d-44-a0 ceilometer-polling: Instance 213e62de-5b97-47c2-90f3-3afa794bafbb was shut off while getting samples of MemoryUsagePollster: Failed to inspect data of instance <name=instance-0003c936, id=213e62de-5b97-47c2-90f3-3afa794bafbb>, domain state is SHUTOFF.
# ubuntu
Sep 21 06:25:36 ctl01 haproxy[24057]: 172.16.10.102:41194 [21/Sep/2017:06:25:36.349] mysql_cluster mysql_cluster/ctl01 1/0/519 3680 -- 105/97/97/97/0 0/0
Sep 21 06:25:37 ctl01 haproxy[24057]: 172.16.10.102:41132 [21/Sep/2017:06:25:33.786] mysql_cluster mysql_cluster/ctl01 1/0/3571 8885 -- 104/96/96/96/0 0/0
Sep 21 06:25:37 ctl01 haproxy[24057]: Server keystone_admin_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue.
Sep 21 06:25:37 ctl01 haproxy[24057]: Server keystone_admin_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 1 sessions active, 0 requeued, 0 remaining in queue.
Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.103:35860 [21/Sep/2017:06:25:35.639] mysql_cluster mysql_cluster/ctl01 1/0/2567 8885 -- 104/96/96/96/0 0/0
Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.102:41230 [21/Sep/2017:06:25:37.839] mysql_cluster mysql_cluster/ctl01 1/0/618 3680 -- 104/96/96/96/0 0/0
Sep 21 06:25:38 ctl01 haproxy[24057]: 172.16.10.103:35920 [21/Sep/2017:06:25:38.384] mysql_cluster mysql_cluster/ctl01 1/0/526 3680 -- 103/95/95/95/0 0/0
Sep 21 06:25:39 ctl01 haproxy[24057]: Server cinder_api/ctl01 is DOWN, reason: Layer7 timeout, check duration: 10001ms. 2 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
"""##
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