import Foundation
let pattern = #"(?=.*\{myVariable1\})(?=.*<html>)(?!.*<script>)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = ##"""
Recycle Bin <scrippt>
Assigned to me 18 work items (1 selected)
ResultsEditorCharts Work item paneBottom
Save query
Column options {myVariable1}
Filter
IDWork Item TypeTitleStateRemaining WorkSeverityCreated Date705801Bug Ipad Tablet_PM Planning Labor Grid: Show Cost/Billing dropdown menu is overlapped by Plan Settings menu in PM workspace >> Planning Labor gridCommitted 38/24/2016 11:49:02 PM705969Bug Android Tablet_PM Planning Labor Grid: Columns displayed in Labor middle grid are overlappingCommitted 38/25/2016 4:56:55 AM705973Bug Android Tablet_PM Planning Labor Grid: Available Columns and Selected Columns grids are not displayed in Labor Grid SettingsCommitted 38/25/2016 5:08:06 AM712031Bug Column settings do not use the correct defaultsCommitted 39/14/2016 12:22:09 PM716142Bug Ipad Tablet_PM Plan/Project mode: List of Projects are not displayed properly in Project Result list dropdown menuCommitted 39/28/2016 2:06:17 AM707226Task Dev: Client - Remove Project Settings option and move individual settings to other placesDone 8/30/2016 4<html>35:35 PM619943Task Dev: Client - Actions popup menuDone 7/1/2016 2:03:11 PM693953Task dev:fix options above grid (PBI 612771)Done 7/26/2016 1:36:11 PM696198Task Dev: Changes to grid column selection dialog to support cost and billing views.Done 8/1/2016 1:37:43 PM699509Bug # selections above grid doesnt appear when paging is appliedIn Development 38/9/2016 12:46:11 PM699660Bug # selection doesn't show when "select All" checkbox is disabledIn Development
"""##
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