import Foundation
let pattern = #"New Account:\n\s+Security ID:[^\n]+\n\s+Account Name:\s+(?<account_created>[^\n]+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
6/6/19
9:27:22.000 AM
06/06/2019 09:27:22 AM
LogName=Security
SourceName=Microsoft Windows security auditing.
EventCode=4720
EventType=0
Type=Information
ComputerName=CPMASNAAD03.na.cintas.com
TaskCategory=User Account Management
OpCode=Info
RecordNumber=5472484169
Keywords=Audit Success
Message=A user account was created.
Subject:
Security ID: "xxxxxxxxx"
Account Name: Account Creator
Account Domain: xxxxx
Logon ID: xxxxxxx
New Account:
Security ID: "xxxxxx"
Account Name: Account Created
Account Domain: xxxxxxx
Attributes:
SAM Account Name: xxxxxxxx
Display Name: User
User Principal Name: -
Home Directory: -
Home Drive: -
Script Path: -
Profile Path: -
User Workstations: -
Password Last Set: <never>
Account Expires: <never>
Primary Group ID: 513
Allowed To Delegate To: -
Old UAC Value: 0x0
New UAC Value: 0x11
User Account Control:
Account Disabled
'Normal Account' - Enabled
User Parameters: -
SID History: -
Logon Hours: <value not set>
Additional Information:
Privileges
"""#
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