import Foundation
let pattern = #","(?<date>\d\d\d\d-\d\d-\d\d)\ (?<time>\d\d:\d\d:\d\d).*\[(?<jail>sshd|recidive|mysqld-auth)\]\ (?<action>[a-zA-z]*)\ (?<ip_address>[\d\.]*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
@timestamp,@message
2021-04-30 18:17:08.504,"2021-04-30 19:17:04,189 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.223 - 2021-04-30 19:17:03"
2021-04-30 18:11:24.504,"2021-04-30 19:11:20,137 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.198 - 2021-04-30 19:11:19"
2021-04-30 18:04:24.504,"2021-04-30 19:04:19,434 fail2ban.filter [100432]: INFO [sshd] Found 221.131.165.56 - 2021-04-30 19:04:19"
2021-04-30 18:03:04.504,"2021-04-30 19:02:59,705 fail2ban.filter [100432]: INFO [sshd] Found 213.171.212.141 - 2021-04-30 19:02:59"
2021-04-30 17:58:11.504,"2021-04-30 18:58:06,901 fail2ban.filter [100432]: INFO [recidive] Found 205.185.119.236 - 2021-04-30 18:58:06"
2021-04-30 17:58:07.132,"2021-04-30 18:58:06,628 fail2ban.actions [100432]: NOTICE [sshd] Ban 205.185.119.236"
2021-04-30 17:58:06.631,"2021-04-30 18:58:06,208 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.380,"2021-04-30 18:58:06,205 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:57:40.504,"2021-04-30 18:57:35,482 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.143 - 2021-04-30 18:57:35"
2021-04-30 17:41:27.504,"2021-04-30 18:41:23,069 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.135 - 2021-04-30 18:41:22"
2021-04-30 17:40:09.504,"2021-04-30 18:40:05,206 fail2ban.filter [100432]: INFO [sshd] Found 222.187.239.107 - 2021-04-30 18:40:04"
2021-04-30 17:38:16.504,"2021-04-30 18:38:11,847 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.151 - 2021-04-30 18:38:11"
"""#
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