import Foundation
let pattern = #"\d+(?!\s)\D"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
\d+(?!\W)\D
.\d+(?!\s)\D
[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+
[0-9]+[a-zA-Z]
25 - not this number
the cow just over the moon and the sun is in 1the sky
26 - not this number
5one day is soon and soon is near take 5529care, 30over and out
59 - not this number
The covers at near the back of the 59closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the 9second shelve.
60 not this number
The hand sanitizer 30was actually clear glue.
61 not this number
I only enjoy window 16shopping when the 4windows are transparent.
62 not this number
The Great Dane looked more like a horse than 2a dog.
63 not this number
If you spin around three times, you'll start 150to feel melancholy.
160 not this number
When 8I was little I had a car door slammed shut on my 7hand and I still remember it quite 72vividly.
172 not this number
I would have gotten the promotion, but my attendance wasn’t good enough 17for movement.
"""#
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