import Foundation
let pattern = #"([A-Z][a-z]{2} [A-Z][a-z]{2} \d{2} \d{2}:\d{2}:\d{2} CET \d{4})\n(/dev.*?) ==> (.*?)\n.*?\n(\d*) bytes .*? copied, (.*?) s, (.*?) (.*?s\n)(.*?)Exit status: (\d*)\n"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
Thu Feb 11 15:01:37 CET 2021
/dev/mapper/vgdata-euramis_u000 ==> /ec/prod/server/euramis/u000
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00383015 s, 134 kB/s
Command being timed: "dd if=/dev/zero of=/ec/prod/server/euramis/u000/TstPerf/test_512_1 bs=512 count=1 oflag=dsync"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 25%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 812
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 252
Voluntary context switches: 4
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 8
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Thu Feb 11 16:02:17 CET 2021
/dev/mapper/vgdata-euramis_u000 ==> /ec/prod/server/euramis/u000
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00275458 s, 186 kB/s
Command being timed: "dd if=/dev/zero of=/ec/prod/server/euramis/u000/TstPerf/test_512_1 bs=512 count=1 oflag=dsync"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 33%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 816
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 253
Voluntary context switches: 4
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 8
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Thu Feb 11 17:02:26 CET 2021
/dev/mapper/vgdata-euramis_u000 ==> /ec/prod/server/euramis/u000
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00323523 s, 158 kB/s
Command being timed: "dd if=/dev/zero of=/ec/prod/server/euramis/u000/TstPerf/test_512_1 bs=512 count=1 oflag=dsync"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 25%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 820
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 252
Voluntary context switches: 4
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 8
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
"""#
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