import Foundation
let pattern = #"((maildir=cipec\.com\.br.*\/)|(clearpasswd.*\,))"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
3-Sep 1 08:40:59 srvmail authdaemond: password matches successfully
4-Sep 1 08:40:59 srvmail authdaemond: authmysql: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=helio@cipec.com.br, fullname=<null>, maildir=cipec.com.br/helio/, quota=<null>, options=<null>
5:Sep 1 08:40:59 srvmail authdaemond: authmysql: clearpasswd=<null>, passwd=pdsPDnYTP3gyw
6-Sep 1 08:40:59 srvmail authdaemond: Authenticated: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=helio@cipec.com.br, fullname=<null>, maildir=cipec.com.br/helio/, quota=<null>, options=<null>
7:Sep 1 08:40:59 srvmail authdaemond: Authenticated: clearpasswd=hel733, passwd=pdsPDnYTP3gyw
8-Sep 1 08:40:59 srvmail pop3d: LOGIN, user=helio@cipec.com.br, ip=[::ffff:10.0.1.103], port=[46999]
9-Sep 1 08:40:59 srvmail pop3d: LOGOUT, user=helio@cipec.com.br, ip=[::ffff:10.0.1.103], port=[46999], top=0, retr=0, rcvd=12, sent=39, time=0
--
42-Sep 1 08:41:02 srvmail authdaemond: password matches successfully
43-Sep 1 08:41:02 srvmail authdaemond: authmysql: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=antonio@cipec.com.br, fullname=<null>, maildir=cipec.com.br/antonio/, quota=<null>, options=<null>
44:Sep 1 08:41:02 srvmail authdaemond: authmysql: clearpasswd=<null>, passwd=pdiHUG4qjaDnI
45-Sep 1 08:41:02 srvmail authdaemond: Authenticated: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=antonio@cipec.com.br, fullname=<null>, maildir=cipec.com.br/antonio/, quota=<null>, options=<null>
46:Sep 1 08:41:02 srvmail authdaemond: Authenticated: clearpasswd=ant527, passwd=pdiHUG4qjaDnI
47-Sep 1 08:41:02 srvmail pop3d: LOGIN, user=antonio@cipec.com.br, ip=[::ffff:10.0.1.103], port=[47004]
48-Sep 1 08:41:02 srvmail postgrey[1708]: action=pass, reason=client AWL, client_name=cipec.com.br, client_address=201.48.59.65, sender=return@bonustelecom.com.br, recipient=bianca@cipec.com.br
--
73-Sep 1 08:41:04 srvmail authdaemond: password matches successfully
74-Sep 1 08:41:04 srvmail authdaemond: authmysql: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=davi@cipec.com.br, fullname=<null>, maildir=cipec.com.br/davi/, quota=<null>, options=<null>
75:Sep 1 08:41:04 srvmail authdaemond: authmysql: clearpasswd=<null>, passwd=u3WErgO7RPtDc
76-Sep 1 08:41:04 srvmail authdaemond: Authenticated: sysusername=<null>, sysuserid=5000, sysgroupid=5000, homedir=/home/vmail, address=davi@cipec.com.br, fullname=<null>, maildir=cipec.com.br/davi/, quota=<null>, options=<null>
77:Sep 1 08:41:04 srvmail authdaemond: Authenticated: clearpasswd=dav510, passwd=u3WErgO7RPtDc
78-Sep 1 08:41:04 srvmail pop3d: LOGIN, user=davi@cipec.com.br, ip=[::ffff:10.0.1.103], port=[47017]
79-Sep 1 08:41:04 srvmail pop3d: LOGOUT, user=davi@cipec.com.br, ip=[::ffff:10.0.1.103], port=[47017], top=0, retr=0, rcvd=12, sent=39, time=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