import Foundation
let pattern = #"(?ms)(^NAME.*?)^\s+^\d"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 27 13:46:07 2016
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
NAME
--------------------------------------------------------------------------------
Agent-Job Follow Up Missed Group D
Agent-Job Follow Up Missed Group E
Audit Archive
Day BGO - 705 and 705NA - A (2)
Day BGO - ERR Quebec - A
Dispatch Lunch Break Agent (Pre-Agent) - Group D
Helper Job Agent (Amend, Prevent, Lead) Group A
Incomplete Tasks Display Status Cleanup - Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group A
Jeopardy Agent (Late EnRoute, OnSite, Complete) Group B
Lunch Break Report Agent(Post-Agent) - Group A
NAME
--------------------------------------------------------------------------------
MST Validation - Group A
MST Validation - Group C
Unschedule INHS Tasks
Unschedule Jobs with Schedule Update Failure - Group E
15 rows selected.
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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