import Foundation
let pattern = #"^2$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
# ___ ___ ___ ___ ___
# | _ )_ ) __| / __| __ _ _ ___ ___ _ _ | _ \___ ___
# | _ \/ /\__ \ \__ \/ _| '_/ -_) -_) ' \| / -_|_-<
# |___/___|___/__|___/\__|_| \___\___|_||_|_|_\___/__/
# |_ _| __| \/ | _ \ | /_\_ _| __|
# | | | _|| |\/| | _/ |__ / _ \| | | _|
# |_| |___|_| |_|_| |____/_/ \_\_| |___|
#
# PLEASE MODIFY FOR YOUR OWN USAGE
# Playfield Screen resolution width/height
1920
1080
# Backglass Screen resolution width/height
1280
1024
# Define Backglass using Display Devicename screen number (\\.\DISPLAY)x or screen coordinates (@x) or screen index (=x)
2
# x position For the backglass relative To the upper left corner Of the Playfield screen
0
# y position For the backglass On the selected display (Normally left at 0)
0
# width/height Of the DMD area In pixels - For 3 screen setup
551
189
# X/Y position Of the DMD area relative To the upper left corner Of the backglass screen - For 3 screen setup
367
813
# Y-flip, flips the LED display upside down
0
# X/Y position pos When StartBackground Is active, relative To upper left corner Of Playfield ('Small' Button In the Options)
0
0
# width/height Of the backglass When StartBackground Is active
0
0
# C:\path\Frame (The path To the location where you have the background image)
"""##
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