import Foundation
let pattern = #"(\B(\?\?|\?)|\B(\!\!|\!)|\B(\[\])|\b(RWC)|\b(TODO))[:;.,-]?\d*($|\s.*$|\(.*$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = ##"""
# Oh yeah, good luck with that.
?? Can I use the gun?
Enough about your promiscuous mother, Hermes! We have bigger problems.
!!- Guards! Bring me the forms I need to fill out to have her taken away!
TODO Shinier than yours, meatbag.
- Wow, you got that off the Internet?
- In my day, the Internet was only used to download pornography.
Would you censor the Venus de Venus just because you can see her spewers?
! No argument here.
RWC: Oh no! The professor will hit me!
[] But if Zoidberg 'fixes' it… then perhaps gifts!
I was all of history's great robot actors - Acting Unit 0. 8; Thespomat; David Duchovny!
[] Five hours? Aw, man! Couldn't you just get me the death penalty?
When I was first asked to make a film about my nephew, Hubert Farnsworth, I thought "Why should I?" Then later, Leela made the film. But if I did make it, you can bet there would have been more topless women on motorcycles. Roll film! Um, is this the boring, peaceful kind of taking to the streets?
"""##
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