import Foundation
let pattern = #"_(.*?)(?=\\)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
{\"header\":[[{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Opportunity<\\/p>\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Date<\\/p>\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Launch Vehicle<\\/p>\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Excess Capacity<\\/p>\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Orbit<\\/p>\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"text\",\"text\":\"<p>Cost<\\/p>\",\"type\":\"Paragraph\"}]}]],\"style\":\"normal\",\"body\":[[{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"name\",\"source\":\"_18_0_2_f280366_1447380313181_755886_144674\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"value\",\"source\":\"_18_0_2_f280366_1447380432770_58741_154478\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"value\",\"source\":\"_18_0_2_f280366_1447380396246_873072_144853\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"value\",\"source\":\"_18_0_2_f280366_1447380432768_736282_154472\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"value\",\"source\":\"_18_0_2_f280366_1447380395593_804641_144677\",\"type\":\"Paragraph\"}]},{\"colspan\":\"1\",\"rowspan\":\"1\",\"content\":[{\"sourceType\":\"reference\",\"sourceProperty\":\"value\",\"source\":\"_18_0_2_f280366_1447380433550_448862_154654\",\"type\":\"Paragraph\"}]}]],\"title\":\"Possible Rideshares\",\"type\":\"Table\"}
IShouldGet3Match,allTheSequenceBetweenaUnderScoreAndASlashHere'sOne:_1234_1234\andheresAnother_1234_1234_1234\OhLookAnotherOne!_123_12_1234567\bahbahba
"""#
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